Saving a photo and using it immediately

前端 未结 3 912
悲哀的现实
悲哀的现实 2021-01-17 04:33

I have the following code for taking a photo and saving it to the camera roll. I need to be able to use it there and then after saving without having to go back into the gal

相关标签:
3条回答
  • 2021-01-17 05:19

    You can get Path of the recently added image by camera this way:

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        imagePicked.image = image
       if (cameraBool){
            var imageData = UIImageJPEGRepresentation(imagePicked.image, 0.6)
            var compressedJPGImage = UIImage(data: imageData)
            ALAssetsLibrary().writeImageToSavedPhotosAlbum(compressedJPGImage!.CGImage, orientation: ALAssetOrientation(rawValue: compressedJPGImage!.imageOrientation.rawValue)!,
            completionBlock:{ (path:NSURL!, error:NSError!) -> Void in
                println("\(path)")  //Here you will get your path
            })
        }
        self.dismissViewControllerAnimated(true, completion: nil);
    }
    

    Which will print path like this:

    assets-library://asset/asset.JPG?id=C5E0EB97-5D3D-41F9-8782-F48140144432&ext=JPG
    
    0 讨论(0)
  • 2021-01-17 05:24

    Use the ALAssetsLibrary, if you save the image using UIImageWriteToSavedPhotosAlbum, you don't get a pointer to it. More info.

    Edit: I think the solution of @DharmeshKheni should work. My guess is that you read the cameraPath before this is set. In other words: I think you try to use the new picture before it's saved. Can you tell where the cameraPath is set, and where you read it?

    0 讨论(0)
  • 2021-01-17 05:27

    You can use protocols/Delegation-pattern for this. See the Apple Doc.

    Basically you create a Protocol in your class with the requiered functions, set a delegate variable, and then hand off your Image to the new View Controller. You can find a tutorial here.

    0 讨论(0)
提交回复
热议问题