2014-01-13 85 views
1

我試着在代碼下面選擇視頻時獲取視頻的大小,但我的應用程序已經崩潰。我想要從ALAsset獲取每個視頻的大小,然後將它們添加到Array。怎麼可以做到這一點?請給我一些建議。謝謝。如何從ALASSET獲取視頻的大小ios

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

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

或使用ALAssetRepresentation大小是,在用於表示該文件的字節返回的大小。

+0

謝謝,我已經做了。 – user3168540

3

下面是一個完整的代碼來獲得媒體長度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]); 
} 
相關問題