一.使用系统的Assets Library Framework
这个是用来访问Photos程序中的图片和视频的库。其中几个类解释如下:
- ALAsset:包含一个图片或视频的各种信息
- ALAssetRepresentation:得到ALAsset的各种信息
- ALAssetsFilter:用来从一个ALAssetsGroup中检索ALAssets
- ALAssetsGroup:一组ALAsset,一个asset可以属于多个这样的组,可以添加一个asset到某个组中
- ALAssetsLibrary:整个图片库中的内容,可以对图片库的获取与编辑等
网上有人说这种方法会要求授权地理位置信息,不过我没有遇到...
看看官方的使用示例,枚举的时候以nil结束哦,记得判断处理下。
/ The following example shows how you can get an asset to represent the first video in the Saved Photos Album.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just videos.
[group setAssetsFilter:[ALAssetsFilter allVideos]];
// For this example, we're only interested in the first item.
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0]
options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
NSURL *url = [representation url];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
// Do something interesting with the AV asset.保存视频url 获取视频缩略图等等
}}];}failureBlock: ^(NSError *error) {
NSLog(@"No groups");}];[library release];
//iOS 获取本地视频的缩略图
+(UIImage *)getImage:(NSString *)videoURL
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return thumb;
}