Get the local file path of a PHAsset

I have a photo I want my user to be able to share on Instagram and I need to get the local file directory of the photo.

However I am fetching my images as a PHAsset (not the ALAsset all the other answers seem to cover on this subject).

  • Expand and Collapse tableview cells
  • What is Swift.AnyClass?
  • Attempt to present UIViewController while a presentation is in progress!-Warning
  • iOS Background Fetch Not Working Even Though Correct Background Mode Configured
  • Full swipe UITableViewCell to delete UITableView iOS 8
  • How to take a picture and save in SQLite database on iOS
  • Looking at the PHAsset documentation I don’t see a ‘local directory’ variable.

    Do you know how I can get the path to a photo from the users camera roll using the new ios8 Photo’s framework?

    Here is my code for loading the last image in the users photo roll

    public func loadLastPhotoIntoGalleryIcon()
    {
        if(PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.Authorized)
        {
            return
        }
        var fetchOptions:PHFetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        var fetchResult:PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
    
        var lastAsset:PHAsset = fetchResult.firstObject as PHAsset
    
        var options:PHImageRequestOptions = PHImageRequestOptions()
        options.version = PHImageRequestOptionsVersion.Current
        PHImageManager.defaultManager().requestImageForAsset(lastAsset, targetSize: _view.getGalleryIconSize(), contentMode: PHImageContentMode.AspectFill, options: options)
        {
            (result, objects) -> Void in
            self._view.setGalleryIcon(result)
        }
    
    }
    

    3 Solutions Collect From Internet About “Get the local file path of a PHAsset”

    Use content editing input:

    asset.requestContentEditingInputWithOptions(PHContentEditingInputRequestOptions()) { (input, _) in
        let url = input.fullSizeImageURL
    }
    

    Make sure that the asset is PHAssetMediaType.Image or otherwise it may crash when unpacking an optional.

    Yes, you must convert PHAsset to ALAsset, then you can grab the url from ALAsset.
    Something like this:
    PHImageManager.defaultManager().requestAVAssetForVideo….

    Requests asset information for beginning a content editing session.

    - (PHContentEditingInputRequestID)requestContentEditingInputWithOptions:(PHContentEditingInputRequestOptions *)options 
                                                          completionHandler:(void (^)(PHContentEditingInput *contentEditingInput, NSDictionary *info))completionHandler;
    

    The URL to a file that contains the full-sized image data.

    @property(readonly, copy) NSURL *fullSizeImageURL;