mit*_*ece 13 objective-c gif photos ios phasset
我不知道为什么这么难.我正在尝试确定PHAsset的文件类型,具体来说,我想知道给定的资产是否代表GIF图像.
只需检查资产的文件名告诉我它是MP4:
[asset valueForKey:@"filename"] ==> "IMG_XXXX.MP4"
iOS保存到设备图像库时,是否会将GIF转换为视频?我也尝试获取图像的数据并查看它的dataUTI,但它只返回nil
GIF(我也假设所有视频).我正在获取图像数据如下:
PHImageManager *manager = asset.imageManager ? asset.imageManager : [PHImageManager defaultManager];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
PHImageRequestOptions *o = [[PHImageRequestOptions alloc] init];
o.networkAccessAllowed = YES;
[manager requestImageDataForAsset:asset.asset options:o resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
dispatch_async(dispatch_get_main_queue(), ^{
CIImage *ciImage = [CIImage imageWithData:imageData];
if(completion) completion(imageData, dataUTI, orientation, info, ciImage.properties);
});
}];
});
Run Code Online (Sandbox Code Playgroud)
从上面的调用返回的dataUTI是nil
.
如果有人知道确定PHAsset文件类型的可靠方法(我特意寻找GIF,但能够确定任何类型的文件会很棒)请告诉我!
小智 11
使用PHAssetResource.
NSArray *resourceList = [PHAssetResource assetResourcesForAsset:asset];
[resourceList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAssetResource *resource = obj;
if ([resource.uniformTypeIdentifier isEqualToString:@"com.compuserve.gif"]) {
isGIFImage = YES;
}
}];
Run Code Online (Sandbox Code Playgroud)
小智 6
您还可以从PHContentEditingInput类中找到uniformTypeIdentifier.为了这; 使用PHAsset中的requestContentEditingInput函数
不要忘记为kUTTypeGif导入MobileCoreServices
示例Swift 3.1代码:
let options = PHContentEditingInputRequestOptions()
options.isNetworkAccessAllowed = true //for icloud backup assets
let asset : PHAsset = ..... //sampleAsset
asset.requestContentEditingInput(with: options) { (contentEditingInput, info) in
if let uniformTypeIdentifier = contentEditingInput?.uniformTypeIdentifier {
if uniformTypeIdentifier == (kUTTypeGIF as String) {
debugPrint("This asset is a GIF")
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于 Swift 3.0 及以上
import MobileCoreServices
var isGIFImage = false
if let identifier = asset.value(forKey: "uniformTypeIdentifier") as? String
{
if identifier == kUTTypeGIF as String
{
isGIFImage = true
}
}
Run Code Online (Sandbox Code Playgroud)
小智 2
首先,我不知道你所说的GIF图像是什么意思。
您指的是实时照片还是延时摄影?
但是,如果您想检查当前资源是Live Photo、Time-lapse,那么您可以这样检查
if(asset.mediaSubtypes == PHAssetMediaSubtypePhotoLive)
{
// this is a Live Photo
}
if(asset.mediaSubtypes == PHAssetMediaSubtypeVideoTimelapse)
{
// this is a Time-lapse
}
Run Code Online (Sandbox Code Playgroud)
要确定 PHAsset 的通用文件类型,您可以检查
asset.mediaType == PHAssetMediaTypeImage
asset.mediaType == PHAssetMediaTypeVideo
asset.mediaType == PHAssetMediaTypeAudio
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6127 次 |
最近记录: |