使用Photos Framework获取相机拍摄的照片的URL [英] Getting the URL of picture taken by camera with Photos Framework

查看:147
本文介绍了使用Photos Framework获取相机拍摄的照片的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 UIImagePickerController 的应用从相机和照片库中检索图片。
在图像选择器委托中,我只想保存所选图像的NSURL( UIImagePickerControllerReferenceURL )以节省内存。当用户稍后需要查看图像时,我会直接从照片库中使用PHCachingImageManager加载它。

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.

现在 - 整个过程非常适合用户选择的图片图书馆,但不是相机直接拍摄的照片(因为没有网址)。我目前正在尝试使用 PHAsset 保存图片,但我不知道如何获取保存图片的NSURL。

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?

最好,
Georg

Best, Georg

推荐答案

更新:好像我找到了这个问题的答案。

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

第1步:我将图像保存到相机

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.

步骤2:操作完成后,我会执行以下操作:

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

我使用PHAsset获取相机胶卷中的最后一张图像并保存图像的本地标识符。这不是URL,而是不会更改的唯一标识符。这样,您就可以完美地访问保存的图像。

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!

这篇关于使用Photos Framework获取相机拍摄的照片的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆