从斯威夫特的画廊中挑选图片

时间:2015-05-26 14:46:56

标签: ios swift

我在视图控制器中有一个图像视图。我想在用户从图库中选择图像时设置此图像。

我试过这段代码:

@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
}

它将调试消息打印到控制台,但它不会更改图像。我该如何解决这个问题?

5 个答案:

答案 0 :(得分:0)

您只是忘了将图库中的图片分配到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
}

答案 1 :(得分:0)

你可以尝试这段代码:

@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
                }
        }

答案 2 :(得分:0)

        @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.")
}

答案 3 :(得分:0)

要使用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 

查看文档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)
}

希望有所帮助

答案 4 :(得分:0)

不推荐使用您正在使用的委托方法。您应该将其替换为:

- (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.