如何确定 iOS8 中视频 PHAsset 磁盘上的文件大小 [英] How can I determine file size on disk of a video PHAsset in iOS8

查看:62
本文介绍了如何确定 iOS8 中视频 PHAsset 磁盘上的文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 iOS8 中的照片框架请求视频 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:

For 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 方法.

Edit For PHAsset, another way to get file size. But as @Alfie Hanssen mentioned, it works on normal video, for slow motion video, the following method will return a AVComposition asset in the block, so I added the check for its type. For slow motion video, use the requestImageDataForAsset method.

[[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 磁盘上的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆