如何从 ALASSET ios 获取视频大小

Posted

技术标签:

【中文标题】如何从 ALASSET ios 获取视频大小【英文标题】:How to get size of video from ALASSET ios 【发布时间】:2014-01-13 09:06:45 【问题描述】:

我在选择视频时尝试了以下代码来获取视频的大小,但我的应用程序已崩溃。我想从ALAsset 获取每个视频的大小,然后将它们添加到数组中。怎么能这样做?请给我一些建议。谢谢。

    -(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);

【问题讨论】:

【参考方案1】:

这是在 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;
    

注意:大小以字节为单位。将其转换为 MB、GB 等相关表格。

调用下面的方法

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

【讨论】:

【参考方案2】:

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

或使用ALAssetRepresentationsize,它返回文件的大小(以字节为单位)来表示。

【讨论】:

以上是关于如何从 ALASSET ios 获取视频大小的主要内容,如果未能解决你的问题,请参考以下文章

如何将视频从 ALAsset 显示到 UICollectionview ios

iOS PhotoKit框架如何获取视频文件大小

无法使用保管箱 API 将文件(使用 ALAsset 库)上传到保管箱

iOS 在 UITableView 中显示来自 ALAsset 的图像

如何将视频从 ALAsset 显示到 UICollectionview Cell

ALAsset 中缺少视频文件缩略图时间戳