Get the local file path of a PHAsset

后端 未结 6 1659
后悔当初
后悔当初 2021-02-18 21:31

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

相关标签:
6条回答
  • 2021-02-18 22:10

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

    0 讨论(0)
  • 2021-02-18 22:17

    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;
    
    0 讨论(0)
  • 2021-02-18 22:21

    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.

    0 讨论(0)
  • 2021-02-18 22:25
    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.

    0 讨论(0)
  • 2021-02-18 22:27

    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

    0 讨论(0)
  • 2021-02-18 22:30
    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"]
    
    }];
    
    0 讨论(0)
提交回复
热议问题