简体   繁体   中英

Selecting image from library doesn't call “didFinishPickingImage” function - Swift

I have a UIImagePickerController with the option to select from library.

When the accessLibrary button is called, it prompts another UIImagePickerController that has source type .PhotoLibrary .

func accessLibrary(sender: UIButton!) {
    NSLog("access library")
    choseFromLibrary = true
    self.libraryPicker = UIImagePickerController()
    self.libraryPicker!.allowsEditing = false
    self.libraryPicker!.sourceType = .PhotoLibrary

    self.presentViewController(self.libraryPicker!, animated: true, completion: nil)
}

This all works well, but when the user does select a photo, I want to pass that image to a view and then show that view on the screen.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary
    var image = info[UIImagePickerControllerOriginalImage] as? UIImage
    if (image != nil) {
        if (choseFromLibrary) {
            let cnfrmImgView = ConfirmImageView.init(image: image!)
            self.view.addSubview(cnfrmImgView)
        }
        else {
            if (frontFacing) {
                image! = UIImage(CGImage: image!.CGImage!, scale: 1.0, orientation: .LeftMirrored)
            }
            let cnfrmImgView = ConfirmImageView.init(image: image!)
            self.view.addSubview(cnfrmImgView)
        }
    }
}

The above function works if the image is taken using the camera, but not using the library.

When a user picks a photo from the library, the library picker controller just dismisses itself and the view is not display.

I ran an NSLog("here") to check if the didFinishPickingImageWithInfo function is called when choosing from the library and in fact it is not.

didFinishPickingImageWithInfo is a delegate message ( UIImagePickerControllerDelegate ). The problem, however, is that you have not given your image picker any delegate ! Thus, it has no one to send delegate messages to.

if you want to implement a Instagram like image picker than try this

Image Picker like Instagram

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM