How to Determine File Size on Disk of a Video Phasset in iOS8

How can I determine file size on disk of a video PHAsset in iOS8

Edit

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

}
}];

//original answer

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);
}];

For ALAsset:

ALAssetRepresentation *rep = [asset defaultRepresentation];
float imageSize = rep.size/(1024.0*1024.0);

I tested on one video asset, PHAsset shows the size as 43.703125, ALAsset shows the size as 43.703005.

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
}
}];

Determine image MB size from PHAsset

After emailing the picture to myself and checking the size on the system, it turns out approach ONE is the closest to the actual size.

To get the size of a PHAsset (Image type), I used the following method:

var asset = self.fetchResults[index] as PHAsset

self.imageManager.requestImageDataForAsset(asset, options: nil) { (data:NSData!, string:String!, orientation:UIImageOrientation, object:[NSObject : AnyObject]!) -> Void in
//transform into image
var image = UIImage(data: data)

//Get bytes size of image
var imageSize = Float(data.length)

//Transform into Megabytes
imageSize = imageSize/(1024*1024)
}

Command + I on my macbook shows the image size as 1,575,062 bytes.

imageSize in my program shows the size at 1,576,960 bytes.

I tested with five other images and the two sizes reported were just as close.

Exporting video using PhotoKit (PHAsset) gives different video file every time

UPDATED

It's not clear but I think exporting video from the camera roll does not guarantee fetching same video in every time. So I copied the video from camera roll to my document folder with url (avurlasset.URL) by [NSFileManager copyItemAtURL:toURL:error:] then it copies the same video file in every time. For now it is my final solution.

In this case you have to use requestAVAssetForVideo not requestExportSessionForVideo

So in your case,

PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.version = PHVideoRequestOptionsVersionOriginal;

[[PHImageManager defaultManager] requestAVAssetForVideo:asset
options:options
resultHandler:
^(AVAsset * _Nullable avasset,
AVAudioMix * _Nullable audioMix,
NSDictionary * _Nullable info)
{
NSError *error;
AVURLAsset *avurlasset = (AVURLAsset*) avasset;

// Write to documents folder
NSURL *fileURL = [NSURL fileURLWithPath:tmpShareFilePath];
if ([[NSFileManager defaultManager] copyItemAtURL:avurlasset.URL
toURL:fileURL
error:&error]) {
NSLog(@"Copied correctly");
}
}];

Find the size of photos and videos inside (Photo Library) iOS device using Photos Framework on iOS 8

Use these :

- (NSUInteger)updateVideoCount
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if (status==ALAuthorizationStatusDenied) {
[self goToSettingsAlert];
}
videoCount = 0;
totalVideoSize = 0;

ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];

[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset)
{
NSString *type = [asset valueForProperty:ALAssetPropertyType];
if ([type isEqualToString:ALAssetTypeVideo])
{
videoCount++;

ALAssetRepresentation *rep = [asset defaultRepresentation];
totalVideoSize += rep.size;
}

}
else
{

}
}];
if(group==nil)
{
[self loadTable];
descTable.hidden = NO;
[descTable reloadData];
}
} failureBlock:^(NSError *error) {
}];

return 0;

}

- (NSUInteger)updatePictureCount
{
photoCount = 0;
totalPictureSize = 0;

ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];

[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset)
{
NSString *type = [asset valueForProperty:ALAssetPropertyType];
if ([type isEqualToString:ALAssetTypePhoto])
{
photoCount++;

ALAssetRepresentation *rep = [asset defaultRepresentation];
totalPictureSize += rep.size;
}

}

else
{

}
}];
if(group==nil)
{

[self loadTable];
descTable.hidden = NO;
[descTable reloadData];
}
} failureBlock:^(NSError *error) {

}];

return 0;
}


Related Topics



Leave a reply



Submit