Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the URL of picture taken by camera with Photos Framework

I have an app that uses a UIImagePickerController to retrieve pictures both from camera and from the photos library. In the image picker delegate I only want to save the NSURL (UIImagePickerControllerReferenceURL) of the picked image to save memory. When the user needs to see the image later on, I load it with PHCachingImageManager directly from the photos library.

Now - this whole thing works great with pictures the user chooses from the library, but not with pictures directly taken by camera (since there is no URL). I am currently trying to save the picture with PHAsset, but I have no idea how to get the NSURL of the save picture.

This is what I've been up to:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    picker.dismissViewControllerAnimated(true, completion: nil)

    let pickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    if picker.sourceType == .Camera
    {
        // When taking a picture with the camera, store it in the user roll
        PHPhotoLibrary.sharedPhotoLibrary().performChanges(
            { () -> Void in

                // save the image
                PHAssetCreationRequest.creationRequestForAssetFromImage(pickedImage)

                // TODO how to get the asset url

            }, completionHandler:
            { (finished, error) -> Void in
                if (finished)
                {

                }
            }
        )

    }
    else
    {
        let pickedImageUrl: NSURL? = info[UIImagePickerControllerReferenceURL] as? NSURL

        currentImageUrl = pickedImageUrl

        currentImage = pickedImage

        toggleImageInfoView(true)
        toggleMapState(true)
    }


}

Any ideas how to get the url of the saved picture?

Best, Georg

like image 810
Georg Hennerbichler Avatar asked Feb 19 '16 18:02

Georg Hennerbichler


People also ask

How do I get the URL of an image?

You can get the address for most images you find online. Make sure you're viewing the original version of the image. Some websites will display a thumbnail instead of the whole image. If you get the URL now, you'll only be getting the URL for the thumbnail.

How to capture a single image from a cam in Pygame?

if camlist: # Initialize and start camera cam = pygame.camera.Camera (camlist [0], (640, 480)) cam.start () # capturing the single image image = cam.get_image () # saving the image pygame.image.save (image, "filename.jpg") else: if camera is not detected the moving to this part

How to capture a photo from a live video using HTML?

In the HTML code, we will define a section for streaming the video captured from the camera, where the user will see their live video, to pose, and finally capture a photo. We will have a Take Photo button, which will trigger the photo capturing. And another section in which we will display the output.

How do I get the address of an image?

Find the picture for which you want to get the address. You can get the address for most images you find online. Make sure you're viewing the original version of the image. Some websites will display a thumbnail instead of the whole image. If you get the URL now, you'll only be getting the URL for the thumbnail.


2 Answers

UPDATE: Seems like I found an answer to this Problem.

Step 1: I save the image to the camera

UIImageWriteToSavedPhotosAlbum(image.image, self, #selector(cameraImageSavedAsynchronously), nil)

this is done asynchronously, so make sure to set a selector when operation has finished.

Step 2: When operation has completed, I do the following:

func fetchLastImage(completion: (localIdentifier: String?) -> Void)
{
    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
    fetchOptions.fetchLimit = 1

    let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
    if (fetchResult.firstObject != nil)
    {
        let lastImageAsset: PHAsset = fetchResult.firstObject as! PHAsset
        completion(localIdentifier: lastImageAsset.localIdentifier)
    }
    else
    {
        completion(localIdentifier: nil)
    }
}

I fetch the last image in camera roll with PHAsset and save the local identifier of the image. This is not an URL, but a unique identifier which does not change. This way, you can access the saved image perfectly.

Hope this helps others!

like image 56
Georg Hennerbichler Avatar answered Sep 22 '22 10:09

Georg Hennerbichler


I agree with you.

but, if the Image's Exif has the date of the earlier .

let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
fetchResult.firstObject

fetchResult.firstObject is not the one you just saved.

maybe you can modify fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false) to key: "modificationDate"

BTW, I found an other way:

 __block PHObjectPlaceholder *placeholderAsset = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
    newAssetRequest.location = location;
    newAssetRequest.creationDate = [NSDate date];
    placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
} completionHandler:^(BOOL success, NSError *error) {
    if(success){
        PHAsset *asset = [self getAssetFromlocalIdentifier:placeholderAsset.localIdentifier];
        completionBlock(asset, YES);
    } else {
        completionBlock(nil, NO);
    }
}];

can get the newly PHAsset.

like image 39
yingkong1987 Avatar answered Sep 23 '22 10:09

yingkong1987