简体   繁体   English

在Swift中从图库中选取图片

[英]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.

相关问题 使用ImagePickerViewController从图库中拾取圆形裁剪图像 - Circular crop image while picking it from gallery using ImagePickerViewController 从图库中挑选图像时丢失与设备的连接错误 - lost connection to device error while image picking from gallery 从图库中选取图像后显示View Controller - Present View Controller after picking image from gallery 在iOS中从相机中选取图像时添加图库选项 - add gallery option while picking image from camera in ios 快速检测照片库中已删除的图像 - detect deleted image from photo gallery in swift 通过本地URL(Swift)从库中加载图像 - Loading image from gallery by local URL (Swift) 从图库中选择图像时,image_picker 使应用程序崩溃 IOS,控制台中没有任何日志 - While picking an image from gallery, image_picker is crashing the the app on IOS with no logs in the console 目标c-从图库或摄影机中选取图像后-需要启用旋转,翻转编辑 - objective c - after picking image from gallery or camera - needs to enable rotate, flip editing 如何获取图像路径,快速从图库中选择 - How to get path of image, selected from gallery in swift 从图库中选择图像后如何打开下一个视图控制器(swift) - How to open next viewcontroller after choosing image from gallery (swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM