How to get an ALAsset URL from a PHAsset?

后端 未结 5 1134
灰色年华
灰色年华 2020-11-27 06:36

You can do it sneakily† using the undocumented PHAsset.ALAssetURL property, but I\'m looking for something documented.


† In Objective-C, this will

5条回答
  •  不知归路
    2020-11-27 07:21

    Here is working code tested on iOS 11 both simulator and device

    PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
        [result enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            PHAsset *asset = (PHAsset *)obj;
            [asset requestContentEditingInputWithOptions:nil completionHandler:^(PHContentEditingInput * _Nullable contentEditingInput, NSDictionary * _Nonnull info) {
                NSLog(@"URL:%@",  contentEditingInput.fullSizeImageURL.absoluteString);
                NSString* path = [contentEditingInput.fullSizeImageURL.absoluteString substringFromIndex:7];//screw all the crap of file://
                NSFileManager *fileManager = [NSFileManager defaultManager];
                BOOL isExist = [fileManager fileExistsAtPath:path];
                if (isExist)
                    NSLog(@"oh yeah");
                else {
                    NSLog(@"damn");
                }
            }];
        }];
    

提交回复
热议问题