Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

}
like image 998
Aggressor Avatar asked Feb 04 '15 17:02

Aggressor


2 Answers

Use content editing input:

asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (input, _) in
        let url = input.fullSizeImageURL
}

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

like image 93
Tomasz Bąk Avatar answered Nov 04 '22 09:11

Tomasz Bąk


func getImagesFromAsset(anAsset: PHAsset) {
    PHImageManager.default().requestImage(for: anAsset, targetSize: CGSize(width: 600, height: 600), contentMode: .aspectFill, options: nil, resultHandler: { (image, info) in
        //resultHandler have info ([AnyHashable: Any])
        print(info!["PHImageFileURLKey"])
        if let img = image {
            self.imgv.image = img
        }
    })
}

this might help paste this in you CellForRowAt method and pass the PHAsset in argument.

like image 1
Jayraj Vala Avatar answered Nov 04 '22 11:11

Jayraj Vala