iOS实现图片与视频一次性多选

一.使用系统的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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值