Get the local file path of a PHAsset
Asked Answered
S

6

20

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

}
Shebeen answered 4/2, 2015 at 17:39 Comment(0)
L
15

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.

Linotype answered 5/3, 2015 at 13:41 Comment(1)
input.fullSizeImageURL is not able to load image on device although it is working on simulatorBuxom
D
1
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.

Daigle answered 6/7, 2018 at 13:1 Comment(1)
but you wont able to access directory since apple privacy policyDaigle
M
0

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

Mattoid answered 29/10, 2015 at 8:31 Comment(0)
S
0

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;
Swee answered 17/2, 2017 at 12:53 Comment(0)
I
0
PHAsset *asset = YOUR_ASSET;

PHImageManager *manager = [PHImageManager defaultManager];
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.synchronous = YES;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
options.resizeMode = PHImageRequestOptionsResizeModeNone;
options.networkAccessAllowed = NO;

[manager requestImageForAsset:YOUR_ASSET targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage *resultImage, NSDictionary *info){

    NSURL *filePath = [info valueForKeyPath:@"PHImageFileURLKey"]

}];
Incomparable answered 27/6, 2018 at 3:57 Comment(0)
F
0

asset=PHAsset *asset

[asset requestContentEditingInputWithOptions:[PHContentEditingInputRequestOptions new] completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
   NSURL *imageURL = contentEditingInput.fullSizeImageURL;
}];

Make sure your PHAsset should be an image type to check this PHAssetMediaType.Image should be true

Fleeman answered 15/11, 2018 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.