简体   繁体   中英

How to fetch PHAsset from assetURL in Photos framework?

I want to fetch PHAsset using assetURL. I used the following code :

NSURL *assetURL=[NSURL URLWithString:file.filePath];
        PHFetchResult *fetchResult=[PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil];
        PHAsset *asset= [fetchResult firstObject];

file.filePath has the asset url Im getting from

[[PHImageManager defaultManager]
                             requestImageDataForAsset:asset
                             options:imageRequestOptions
                             resultHandler:^(NSData *imageData, NSString *dataUTI,
                                             UIImageOrientation orientation,
                                             NSDictionary *info)
                             {[uploadingFile setValue:[NSString stringWithFormat:@"%@",[info valueForKey:@"PHImageFileURLKey"]] forKey:@"filePath"];}

But Im always getting the value of fetchResult as nil . Can someone tell me where Im going wrong ? Is it because Im passing the wrong URL type?

you can use this :

 PHContentEditingInputRequestOptions * editOperations = [[PHContentEditingInputRequestOptions alloc]init];
    editOperations.networkAccessAllowed = NO;
    editOperations.canHandleAdjustmentData =^BOOL(PHAdjustmentData * data){
        return NO;
    };

    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [asset requestContentEditingInputWithOptions:editOperations completionHandler:^(PHContentEditingInput * _Nullable contentEditingInput, NSDictionary * _Nonnull info) {
            if (asset.mediaType & PHAssetMediaTypeImage) {
                BOOL  isDownLoading = ![info objectForKey:PHImageCancelledKey]
                && ![info objectForKey:PHImageErrorKey] && ![info objectForKey:PHImageResultIsDegradedKey];
                if (isDownLoading) {
                    mode_asset.mode_fileUrl = [contentEditingInput.fullSizeImageURL absoluteString];
                    mode_asset.mode_UTI = contentEditingInput.fullSizeImageURL.pathExtension;
                    mode_asset.mode_fileName = contentEditingInput.fullSizeImageURL.lastPathComponent;
                }
                if (mode_asset.mode_filePathBlock) {
                    mode_asset.mode_filePathBlock(mode_asset);
                }


            }
                //                dispatch_group_leave(group);

                //                dispatch_semaphore_signal(sema);

        }];
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM