问题描述
我可以使用iOS8中的Photos框架请求视频PHAsset。我想知道文件在磁盘上有多大。似乎没有PHAsset的属性来确定它。有没有人有办法解决吗? (不需要使用照片框架)
I can request a video PHAsset using the Photos framework in iOS8. I'd like to know how big the file is on disk. There doesn't seem to be a property of PHAsset to determine that. Does anyone have a solution? (Using Photos framework not required)
推荐答案
编辑
对于iOS 9.3,在视频类型 PHAsset
上使用 requestImageDataForAsset
将产生一个图像,是视频的第一帧,因此它不再起作用。对于普通视频,请使用以下方法,请求选项可以是 nil
,但对于慢动作视频, PHVideoRequestOptionsVersionOriginal
需要要设置。
As for iOS 9.3, using requestImageDataForAsset
on a video type PHAsset
will result in an image, which is the first frame of the video, so it doesn't work anymore. Use the following method instead, for normal video, request option can be nil
, but for slow motion video, PHVideoRequestOptionsVersionOriginal
needs to be set.
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
AVURLAsset* urlAsset = (AVURLAsset*)asset;
NSNumber *size;
[urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0)); //size is 43.703005
}
}];
//原始回答
对于PHAsset,请使用:
For PHAsset, use this:
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
float imageSize = imageData.length;
//convert to Megabytes
imageSize = imageSize/(1024*1024);
NSLog(@"%f",imageSize);
}];
对于ALAsset:
ALAssetRepresentation *rep = [asset defaultRepresentation];
float imageSize = rep.size/(1024.0*1024.0);
我在一个视频资产上测试过,PHAsset显示大小为43.703125,ALAsset显示大小为43.703005。
I tested on one video asset, PHAsset shows the size as 43.703125, ALAsset shows the size as 43.703005.
编辑
对于PHAsset,另一种获取文件大小的方法。但正如@Alfie Hanssen所说,它适用于普通视频,对于慢动作视频,以下方法将在块中返回AVComposition资源,因此我添加了对其类型的检查。对于慢动作视频,请使用 requestImageDataForAsset
方法。
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
AVURLAsset* urlAsset = (AVURLAsset*)asset;
NSNumber *size;
[urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0)); //size is 43.703005
NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL];
NSLog(@"length %f",[data length]/(1024.0*1024.0)); // data size is 43.703005
}
}];
这篇关于如何确定iOS8中视频PHAsset的磁盘上的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!