Aggressor Aggressor - 1 year ago 264
iOS Question

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


Answer Source

I seemed to be going out the problem the wrong way.

What I did instead was save the image to a temporary directory and then pass that directory to instagram

func shareOnInstagram()
    var image:UIImage = UIImage(named: "myImage.png")!
    var imageData = UIImagePNGRepresentation(image)
    var writePath:String = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo")
    imageData.writeToFile(writePath, atomically: true)
    var fileURL:NSURL = NSURL(fileURLWithPath: writePath)!
    _documentInteractionController = UIDocumentInteractionController(URL: fileURL)
    _documentInteractionController.UTI = "com.instagram.exclusivegram"
    _documentInteractionController.delegate = self
    _documentInteractionController.presentOpenInMenuFromRect(CGRect.zeroRect, inView: ROOT_VIEW_CONTROLLER.view, animated: true)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download