将选中的图像从图库上传到解析

时间:2015-03-13 04:51:31

标签: parsing swift

所以我有这个编译没有错误的代码:

    @IBAction func btnImagePickerClicked(sender: AnyObject){
    let imagePicker = UIImagePickerController()

    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.mediaTypes = [kUTTypeImage as NSString]
    imagePicker.allowsEditing = false

    self.presentViewController(imagePicker, animated: true,
        completion: nil)
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        var user = PFUser.currentUser()
        let image = info[UIImagePickerControllerOriginalImage] as UIImage
        let imageData = UIImageJPEGRepresentation(image, 0.05)
        let imageFile = PFFile(name:"image.jpg", data:imageData)
        user["profilePicture"] = imageFile;
        user.saveInBackgroundWithBlock(nil)

        self.dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

但是它跳过了imagePickerController函数,一旦我选择了一个图像,它就会把我发回给我的主视图控制器

1 个答案:

答案 0 :(得分:1)

您将委托设置为self,因此您需要让自己可以访问这些方法。 当它们嵌套在IBAction内部时,它们只能从其中看到。

采用委托方法并将它们放在IBAction之外。

@IBAction func btnImagePickerClicked(sender: AnyObject){
let imagePicker = UIImagePickerController()

imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.mediaTypes = [kUTTypeImage as NSString]
imagePicker.allowsEditing = false

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

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    var user = PFUser.currentUser()
    let image = info[UIImagePickerControllerOriginalImage] as UIImage
    let imageData = UIImageJPEGRepresentation(image, 0.05)
    let imageFile = PFFile(name:"image.jpg", data:imageData)
    user["profilePicture"] = imageFile;
    user.saveInBackgroundWithBlock(nil)

    self.dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

我测试了它,选择图像后调用了“didFinishPickingMediaWithInfo”委托方法。