I tried below code to get size of video when select video but my app has been crashed. I want to get size of each video is fetched from ALAsset and then add them to Array. How can do that? Please give me some advice. Thanks.

    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = (UICollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    UIImageView *OverlayImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
    OverlayImageView.image = [UIImage imageNamed:@"Overlay-old.png"];
    [cell addSubview:OverlayImageView];
     alasset = [allVideos objectAtIndex:indexPath.row];
     NSDate* date = [alasset valueForProperty:ALAssetPropertyDate];
    NSLog(@"Date Time Modify %@",date);


    //get size of video
    ALAssetRepresentation *rep = [alasset defaultRepresentation];
    Byte *buffer = (Byte*)malloc(rep.size);
    NSError *error = nil;
    NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:&error];
    NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
    NSLog(@"Size of video %@",data);
}
有帮助吗?

解决方案 2

use NSLog(@"Size of video %d",data.length); //length returns the number of bytes contained in the receiver.

or make use of ALAssetRepresentationsize that returns the size in bytes of the file for the representation.

其他提示

Here is a Complete code to get Media length with the help of ALAssetRepresentation

 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *cellIdentifier = @"CollectionCell";
        CollectionCell *cell = [cv dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        ALAsset *asset = self.arrAssetsMedia[indexPath.row];

        //To Show ThumbNailImage
        CGImageRef thumbnailImageRef = [asset thumbnail];
        UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
        cell.cellMediaImage.image = thumbnail;

        //To get the FileSize
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        int Filesize=(int)[rep size];
        [cell.lblMediaSize setText:[self stringFromFileSize:Filesize]];
        return cell;
    }

Note:The size is in the form of Bytes. To convert it to the related Form like MB,GB etc..

Call Below Method

- (NSString *)stringFromFileSize:(int)theSize
{
    float floatSize = theSize;
    if (theSize<1023)
        return([NSString stringWithFormat:@"%i bytes",theSize]);
    floatSize = floatSize / 1024;
    if (floatSize<1023)
        return([NSString stringWithFormat:@"%1.1f KB",floatSize]);
    floatSize = floatSize / 1024;
    if (floatSize<1023)
        return([NSString stringWithFormat:@"%1.1f MB",floatSize]);
    floatSize = floatSize / 1024;

    // Add as many as you like

    return([NSString stringWithFormat:@"%1.1f GB",floatSize]);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top