In my app I have a big UIImageView and other 5 smaller.
My intention is to copy the content of the selected small Image to the biggest one. Also to get the name of the chosen image and show with a UILabel.
Will be maybe easier to replace the small images by UIButtons and display the image as a background?
I would add a gestureRecognizer to each imageView. The function called by the gestureRecognizer would then change the image on the "big picker view". There is no built-in way to get the image name that the user chose. If you really needed this, then you could sublcass UIImageView and add a fileName property.
#IBOutlet var imageView: UIImageView!
#IBOutlet var bigPickerImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// create tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: "tapGesture:")
// add it to the image view;
imageView.addGestureRecognizer(tapGesture)
// make sure imageView can be interacted with by user
imageView.userInteractionEnabled = true
}
func tapGesture(gesture: UIGestureRecognizer) {
// if the tapped view is a UIImageView then set it to imageview
if let imageView = gesture.view as? UIImageView { // if you subclass UIImageView, then change "UIImageView" to your subclass
// change the image on the bigPickerImageView
bigPickerImageView.image = imageView.image
// if you subclass UIImageView, then you could get the filename here.
}
}
Related
I am trying to figure out how to add pinch/zoom capabilities to an imageView,
but specifically to one that I added to the scrollView programmatically. I was able to find some great examples on SO to pinch and zoom an image using a scrollview with viewForZooming, but in that instance I had to return the image view being pinched and zoomed, which doesn't work if I am returning it programatically.
My ultimate goal is to have an array of images where the user can scroll left and right to see all the images AND be able to zoom in on them, basically just as if they were flipping through their photoStream. I found an ok tutorial for adding the images dynamically for scrolling here https://www.codementor.io/taiwoadedotun/ios-swift-implementing-photos-app-image-scrolling-with-scroll-views-bkbcmrgz5#comments-bkbcmrgz5 but I am not clear how to add the viewForZooming since the image views are being .addSubview dynamically in a loop.
I created a little example with a collection view of 0-n images associated to a post. Once the collectionViewCell with the image is tapped a hidden scrollView appears with a new dynamic UIImageView added as a subView. All works great but I don't know how to add the pinch/zoom now.
#objc func imageTapped(_ sender: UITapGestureRecognizer) {
print("BlipeFile Image Tapped")
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
//newImageView.frame = UIScreen.main.bounds
newImageView.contentMode = .scaleAspectFit
newImageView.clipsToBounds = true
newImageView.layer.borderColor = UIColor.gray.cgColor
newImageView.layer.borderWidth = 3.0
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.isUserInteractionEnabled = true
newImageView.image = imageView.image
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
scroller.isHidden = false
scroller.addSubview(newImageView)
}
#objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
scroller.isHidden = true
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
sender.view?.removeFromSuperview()
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return image //Can't return dynamically created newImageView?
}
My ultimate goal is to have an array of images where the user can scroll left and right to see all the images AND be able to zoom in on them
Apple has explained many times, in WWDC videos and in sample code that you can download, how this is done. Basically it’s just a scroll view in a scroll view. The outer scroll view permits scrolling horizontally. The inner scroll view contains an image view and permits zooming.
So instead of adding an image view, add a scroll view containing an image view.
So I have a big image in an UIImageView (Swift) with "Aspect Fill"
But What I want is to have like an Animation or Ken Burn Effect on the Image.
In the Image Below... What I want is to go from Red to Yellow seen the image.
Take a normal uiview give it proper height width as per ur wish... Take an imageview inside of it and give the height, x coordinate and y coordinate same as uiview and keep the width as much wide you want to keep the image....
Use UIView.animateWithDuration method and in its animation block set the final desired frame such that the end of image view comes equal to the end of uiview... The image view will slide outside of vision of the uiview as you desire...
You can even repeatedly animate from left to right calling animation into each others completion block
I found an extension for UIImageView called UIImageViewAligned. You can use the alignment properties provided by the extension and animate the changes.
import UIKit
import UIImageViewAligned
class ViewController: UIViewController {
// MARK: Outlets
#IBOutlet var imageView: UIImageViewAligned!
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
imageView?.contentMode = .scaleAspectFill
imageView?.alignLeft = true
imageView?.alignRight = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 5) {
self.imageView?.alignLeft = false
self.imageView?.alignRight = true
}
}
}
I want to create a login page in which the image I have fills up the entire page. I was able to accomplish this by doing
let imageView = UIImageView(frame: self.view.bounds)
imageView.image = UIImage(named: "background")
self.view.addSubview(imageView)
However, I also have textfields and buttons on my storyboard which I created outlets for on my swift file. When I insert that code into the viewDidLoad() function, the textfields and login button go underneath the image rather than appear over the image. I'm not exactly sure how to get the login page text fields/buttons to appear over the UIImageView rather than be hidden underneath. The code I have for that is written outside the viewDidLoad
(example)
#IBOutlet var usernameTextField: UITextField!
#IBOutlet var passwordTextField: UITextField!
#IBAction func loginPressed() {
let username = usernameTextField.text
let password = passwordTextField.text
Try this:
self.view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
This method moves the specified view to the beginning of the array of views in the subviews property.
This is part of my code.
I want to show two UILabel and two UITextField on the background image: backgroundLogin.jpg .
import Foundation
import UIKit
class LoginViewController:UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
let BackGroundImage:UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width , self.view.frame.height))
let image: UIImage = UIImage(named: "backgroundLogin.jpg")!
BackGroundImage.image = image
self.view.addSubview(BackGroundImage)
//self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundLogin.jpg")!)
username.text = "User name"
password.text = "Password"
}
#IBOutlet weak var username: UILabel!
#IBOutlet weak var password: UILabel!
}
But when this code runs, it looks like:
I hope it can have two label on the image, but how to add that ?
Well like I stated in the comment you should try bring the UILabels to the front.
Since you are doing this programatically it can be done with bringSubviewToFront here is Apple's documentation on this and other UIView functionality you can use. Note: UILabel inherits from UIView so you can use these methods.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/doc/uid/TP40006816-CH3-SW46
The code:
override func viewDidLoad() {
super.viewDidLoad()
let BackGroundImage:UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width , self.view.frame.height))
let image: UIImage = UIImage(named: "backgroundLogin.jpg")!
BackGroundImage.image = image
self.view.addSubview(BackGroundImage)
//self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundLogin.jpg")!)
username.text = "User name"
password.text = "Password"
self.view.bringSubviewToFront(username)
self.view.bringSubviewToFront(password)
}
It seems you are adding the UIImageView in code and the UILabels on storyboard, then when you run the app the labels are behind the UIImageView and you can't see then.
Just check it adding BackGroundImage.hidden = true in viewDidLoad.
You can't add UILabels into a UIImageView. One solution would be add all views in code, 1st the image view and then the labels over it. Other option would be add all then in storyboard in the same order.
Like this:
You’re doing a lot here programmatically that you don’t need to do.
In Storyboard, add a UIImageView to a blank ViewController (you
can rename the view controller later if you wish)
With the new UIImageView selected, control-drag to create an outlet in your view controller named backgroundImage
With the UIImageView selected, choose the image you want it to display by going to the Attribute Inspector and from the dropdown
box, select your image. All images in your project should appear in
the dropdown box.
Now add a UILabel to the ViewController and control-drag to create an outlet in the corresponding code
In the viewDidLoad() method, bring the outlet to the front
All this requires code-wise is:
class ViewController: UIViewController {
#IBOutlet weak var backgroundImage: UIImageView!
#IBOutlet weak var username: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.view.bringSubviewToFront(username)
}
}
I have a UITapGestureReconizer set up with my view but I only want the selector to be called when the view tapped is this picture I have. I tried adding the recognizer to the imageView both programmatically and via the storyboard but neither worked. It only works for my view. Here is the code.
var tapGestureRecognizer:UITapGestureRecognizer?
and in my viewDidLoad I have
tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
self.view.addGestureRecognizer(tapGestureRecognizer!)
my image is a variable as well
#IBOutlet weak var mainImage: UIImageView!
Set the userInteractionEnabled property of the UIImageView to true and then modify your code for that
self.mainImage.addGestureRecognizer(tapGestureRecognizer!)