[英]Picking image from gallery in Swift
I have a image view inside the view controller. 我在视图控制器中有一个图像视图。 I want to set this image when user selects an image from gallery.
当用户从图库中选择图像时,我想设置此图像。
I tried this code: 我尝试了这段代码:
@IBOutlet var profilePicture: UIImageView!
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, completion:nil)
println("image selected from gallery")
self.profilePicture.image = image
}
It prints debug message to console but it is not changing the image. 它将调试消息打印到控制台,但它没有更改映像。 How can I fix this ?
我怎样才能解决这个问题 ?
You just forgot to assign image you got from gallery to your UIImageView
. 您只是忘记了将从图库获得的图像分配给
UIImageView
。
@IBOutlet var profilePicture: UIImageView!
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
profilePicture.image = image
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
println("image selected from gallery")
self.profilePicture.image=image
}
Can you try this code: 您可以尝试以下代码:
@IBOutlet var profilePicture: UIImageView!
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
println("image selected from gallery")
dispatch_async(dispatch_get_main_queue()) {
self.profilePicture.image=image
}
}
@IBAction func chooseProfilePicBtnClicked(sender: AnyObject) {
let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.openCamera()
}
let gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.openGallary()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
{
UIAlertAction in
}
// Add the actions
picker.delegate = self
alert.addAction(cameraAction)
alert.addAction(gallaryAction)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func openCamera(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
picker.sourceType = UIImagePickerControllerSourceType.Camera
self .presentViewController(picker, animated: true, completion: nil)
}else{
let alert = UIAlertView()
alert.title = "Warning"
alert.message = "You don't have camera"
alert.addButtonWithTitle("OK")
alert.show()
}
}
func openGallary(){
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(picker, animated: true, completion: nil)
}
//MARK:UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){
picker .dismissViewControllerAnimated(true, completion: nil)
imageViewRef.image=info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker: UIImagePickerController){
print("picker cancel.")
}
To display a selected image using UIImagePickerController use the following code: 要使用UIImagePickerController显示选定的图像,请使用以下代码:
// Add these protocols into your class declaration
UIImagePickerControllerDelegate, UINavigationControllerDelegate
// Set the delegate
@IBAction func presentViewImagePicker(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
presentViewController(imagePicker, animated: true, completion: nil)
}
// Use these two methods from UIImagePickerControllerDelegate
Check the documentation https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/index.html 检查文档https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/index.html
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageRetrieved.contentMode = .ScaleAspectFill
imageRetrieved.image = pickedImage
}
self.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Hope it helps 希望能帮助到你
The delegate method you are using is deprecated. 您使用的委托方法已弃用。 You should replace it with this:
您应该将其替换为:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info
Use this link to learn which keys you need to use to retrieve the image from the dictionary. 使用此链接来了解需要使用哪些键来从字典中检索图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.