您可以使用undocumented PHAsset.ALAssetURL
属性偷偷地使用†完成此操作,但我正在寻找有文档记录的内容。
在Objective-C中使用†,这将有所帮助
@interface PHAsset (Sneaky)
@property (nonatomic, readonly) NSURL *ALAssetURL;
@end
发布于 2015-03-06 22:00:55
通过利用PHAsset
的本地标识符来创建assetURL。示例:PHAsset.localidentifier
返回91B1C271-C617-49CE-A074-E391BA7F843F/L0/001
现在获取构建assetURL的前32个字符,如下所示:
assets-library://asset/asset.JPG?id=91B1C271-C617-49CE-A074-E391BA7F843F&ext=JPG
您可以根据资产的UTI更改扩展JPG
(requestImageDataForAsset
返回UTI),但在我的测试中,assetURL的扩展似乎被忽略了。
发布于 2015-05-19 13:39:29
我也希望能够获得资产的URL。然而,我已经意识到可以将localIdentifier
持久化,并使用它来恢复PHAsset
。
PHAsset* asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil].firstObject;
旧的资源URL可以使用以下方式进行转换:
PHAsset* legacyAsset = [PHAsset fetchAssetsWithALAssetUrls:@[assetUrl] options:nil].firstObject;
NSString* convertedIdentifier = legacyAsset.localIdentifier;
(在这个方法被淘汰之前...)
(感谢holtmann - localIdentifier
隐藏在PHObject
中。)
发布于 2017-11-14 21:18:50
以下是在iOS 11模拟器和设备上测试的工作代码
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");
}
}];
}];
https://stackoverflow.com/questions/28887638
复制相似问题