我的iphone应用程序可以将照片和视频保存到相机胶卷,我也可以访问相机胶卷中的物品。
显示相机胶卷中的照片很简单,我使用ALAssetsLibrary完成,并且在UIImage中完美显示。我也可以访问视频,并尝试使用MPMoviePlayerController显示视频。
这是我的代码:
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop){
// Within the group enumeration block, filter to enumerate just videos.
[group setAssetsFilter:[ALAssetsFilter allVideos]];
// Chooses the video at the last index
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:([group numberOfAssets]-1)]
options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop){
if (alAsset){
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: [representation url]];
[moviePlayer.view setFrame:CGRectMake(20, 20, 200, 300)];
[self.view addSubview: moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
}
];
}
failureBlock: ^(NSError *error){
// handle error
NSLog(@"No groups");
}
];
我只看到一个黑框,没有视频。
任何人都可以帮助我吗?
感谢。
答案 0 :(得分:0)
看看MPMoviePlayerViewController:
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:[representation url]];
[self presentMoviePlayerViewControllerAnimated:player];
[player.moviePlayer play];
[player release];
答案 1 :(得分:0)
试试这个。我为CollectionViewCell编写了这个,你可以根据需要更新moviePlayerController.view.frame。
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:[[asset defaultRepresentation] url]];
moviePlayerController.view.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);//self.view.frame;
moviePlayerController.movieSourceType= MPMovieSourceTypeFile;
moviePlayerController.fullscreen = YES;
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];