2016-04-18 2 views
1

ALAssetLibraryを使用してビデオのサムネイルを表示し、ギャラリーからアプリにビデオを表示するために、ALAssetsFilterからすべてのビデオをフィルタしました。ALAssetLibraryを使用してギャラリーからビデオをロードするときにアセットの値を取得しない

しかし、私はまだタイプALAssetの資産でnull値を取得しています。

私のコードで間違っていることを教えてください。

助けをお待ちしております。

私はビデオからサムネイル画像を作成してい
-(void)loadAssets{ 
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 

    [group setAssetsFilter:[ALAssetsFilter allVideos]]; 
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *innerStop) { 
       if (asset) 
       { 
        dic = [[NSMutableDictionary alloc] init]; 
        ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation]; 
        NSString *uti = [defaultRepresentation UTI]; 
        videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti]; 
        NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100]; 
        UIImage *image = [self imageFromVideoURL:videoURL]; 
        [dic setValue:image forKey:@"image"]; 
        [dic setValue:title forKey:@"name"]; 
        [dic setValue:videoURL forKey:@"url"]; 
        [allVideos addObject:asset]; 

       } 
      }]; 
     [_collectionView reloadData]; 
    } 

failureBlock:^(NSError *error) 
{ 
    NSLog(@"error enumerating AssetLibrary groups %@\n", error); 
}]; 
} 

- (UIImage *)imageFromVideoURL:(NSURL*)videoURL 
{ 

UIImage *image = nil; 
AVAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];; 
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
imageGenerator.appliesPreferredTrackTransform = YES; 

// calc midpoint time of video 
Float64 durationSeconds = CMTimeGetSeconds([asset duration]); 
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600); 

// get the image from 
NSError *error = nil; 
CMTime actualTime; 
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error]; 

if (halfWayImage != NULL) 
{ 
    // cgimage to uiimage 
    image = [[UIImage alloc] initWithCGImage:halfWayImage]; 
    [dic setValue:image forKey:@"ImageThumbnail"];//kImage 
    NSLog(@"Values of dictonary==>%@", dic); 
    NSLog(@"Videos Are:%@",allVideos); 
    CGImageRelease(halfWayImage); 
} 
return image; 
} 

答えて

1

使用を願っていますこれはサムネイル画像を生成するのに役立ちます -

-(UIImage*)loadImage { 



    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:self.videoURL options:nil]; 



    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 



    generate.appliesPreferredTrackTransform = YES; 



    NSError *err = NULL; 



    CMTime time = CMTimeMake(1, 60); 



    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err]; 



    return [[UIImage alloc] initWithCGImage:imgRef]; 



} 
+0

私はこれを使用しましたが、エラーを取得しています:所有しているALAssetsLibraryの存続期間を過ぎてにアクセスしようとすると、無効な試みが発生しました –

+1

ビデオURLは何ですか?あなたのコードでAVAssetImageGeneratorにAVAssetを渡しています。 AVURLAsset *アセットを渡すか初期化してみてください。このコードはうまく動作します。 –

+1

あなたはまた、エラー解決のためにこれを試すことができます - http://stackoverflow.com/questions/22662464/alassetslibrary-invalid-or-null –

0

、私とコードのためにその作業は以下の通りです、

NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
      NSLog(@"store url %@",videoURL); 
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil]; 

      if ([[avAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0) 
      { 
       AVAssetImageGenerator *imageGenerator =[AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset]; 
       Float64 durationSeconds = CMTimeGetSeconds([avAsset duration]); 
       CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600); 
       NSError *error; 
       CMTime actualTime; 


       CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error]; 


       if (halfWayImage != NULL) 
       { 

        NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime)); 
        NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint)); 
        NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString); 

        UIImage *img=[UIImage imageWithCGImage:halfWayImage]; 

        playButton.hidden=NO; 
        self.myimageView.image= img; //[self scaleImage:img maxWidth:(img.size.width/5) maxHeight:(img.size.height/5)]; 

       } 

      } 

は最後にサムネイル画像を得、このコードは、かもしれないその有用

+0

実際には、私はビデオのサムネイルにアクセスしたいUIImagePickerControllerではなくALAssetLibraryを使用します。私はALAssetLibraryを通して私に提案してください。@ lyyappan Ravi –

+0

はい私はALAssetLibraryとuiimagepickercontrollerも使用しています。 –

+0

高...しかし、私はエラーが発生しました。 –

関連する問題