iOS -- 图片与视频一次性多选

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.
}
}];
}
failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(@"No groups");
}];

[library release];
复制代码

 

二.网络上的另一种方法-MHImagePickerMutilSelector
这个是通过设置UINavigationControllerDelegate的方法,当UIImagePickerController显示在界面上的时候,判断一下当前是图集列表(相当于AlAssetsGroup的列表)还是图片列表(相当于AlAsset的列表),如果是图片列表就调整scrollview的大小,并在下面加上一个自己的滚动视图用来显示已经选择的图片。

这个方法的缺点是对已经选择的图片做点自定义的动作相对而言比较麻烦。

我稍微修改了下^_^(仅仅是稍微,让它调用的时候简单了点,对重复图片什么的也没有处理)

使用时只要UIVIewController实现了 MHImagePickerMutilSelector的协议,然后这样调用就好了。

[MHImagePickerMutilSelector showInViewController:self];

获取IPhone相册中图片的方法(包括获取所有图片)
     
     
分类: IOS开发 Object-C编程语言   2173人阅读  评论(0)  收藏  举报

获取iphone相册方法:

方法一:

ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock =

^(ALAssetsGroup *group, BOOL *stop) {

if (group!=nil) {

[groups addObject:group];

} else {

[self.tableView performSelectorOnMainThread:@selector(reloadData)

withObject:nil waitUntilDone:YES];

}

};

ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error)

{

NSString *errorMessage = nil;

switch ([error code]) {

case ALAssetsLibraryAccessUserDeniedError:

case ALAssetsLibraryAccessGloballyDeniedError:

errorMessage = @"The user has declined access to it.";

break;

default:

errorMessage = @"Reason unknown.";

break;

}

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Opps"

message:errorMessage delegate:self cancelButtonTitle:@"Cancel"

otherButtonTitles:nil, nil];

[alertView show];

[alertView release];

};

NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent |

ALAssetsGroupFaces;

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]  init];

[assetsLibrary enumerateGroupsWithTypes:groupTypes

usingBlock:listGroupBlock failureBlock:failureBlock];

[assetsLibrary release];

方法二:

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) =

^(ALAssetsGroup *group, BOOL *stop)

{

if (group == nil)

{

[self.tableView performSelectorOnMainThread:@selector(reloadData)

withObject:nil waitUntilDone:YES];

return;

}

[groups addObject:group];

};

void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error)

{

NSString *errorMessage = nil;

switch ([error code]) {

case ALAssetsLibraryAccessUserDeniedError:

case ALAssetsLibraryAccessGloballyDeniedError:

errorMessage = @"The user has declined access to it.";

break;

default:

errorMessage = @"Reason unknown.";

break;

}

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Opps"

message:errorMessage delegate:self cancelButtonTitle:@"Cancel"

otherButtonTitles:nil, nil];

[alertView show];

[alertView release];

};

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]  init];

[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll

usingBlock:assetGroupEnumerator

failureBlock:assetGroupEnumberatorFailure];

[assetsLibrary release];

以上是获取所有ALAssetsGroup,即相册对象。

获取相册的标示照片:

ALAssetsGroup *group = [groups objectAtIndex:index];

CGImageRef posterImageRef = [group posterImage];

UIImage *posterImage = [UIImage

imageWithCGImage:posterImageRef];

获取相册中的所有对象(相片和录像等):

group = (ALAssetsGroup*)[assetGroups objectAtIndex:index];

[group setAssetsFilter:[ALAssetsFilter allAssets]];

[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger

index, BOOL *stop)

{

if(result == nil)

{

return;

}

//获取相片的url:

NSString *url = [[[result

defaultRepresentation]url]description];

[self.urls addObject:url];

[self.assets addObject:result];

}];

只获取照片的方法:

ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock =

^(ALAsset *result, NSUInteger index, BOOL *stop) {

if (result) {

[assets addObject:result];

}

};

ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];

[assetsGroup setAssetsFilter:onlyPhotosFilter];

[assetsGroup  enumerateAssetsUsingBlock:assetsEnumerationBlock];

获得的ALAsset对象就是相片对象:其中有相片的缩略图,全屏图,高清图,url等属性。

ALAsset *result = [assets objectAtIndex:index];

获取url:

String类型:

NSString *url = [[[result

defaultRepresentation]url]description];

URL类型:

NSURL *url = [[result defaultRepresentation]url];

获取缩略图:

CGImageRef  ref = [result thumbnail];

UIImage *img = [[UIImage alloc]initWithCGImage:ref];

获取全屏相片:

CGImageRef ref = [[result  defaultRepresentation]fullScreenImage];

UIImage *img = [[UIImage alloc]initWithCGImage:ref];

获取高清相片:

CGImageRef ref = [[result  defaultRepresentation]fullResolutionImage];

UIImage *img = [[UIImage alloc]initWithCGImage:ref];

根据ALAsset的url获取ALAsset对象:

void (^assetRseult)(ALAsset *) = ^(ALAsset *result)

{

if (result == nil)

{

return;

}

UIImage *image = [UIImage imageWithCGImage:[[result  defaultRepresentation]fullScreenImage]];

[self.images addObject:image];

[self.assetArrays addObject:result];

};

void (^failureBlock)(NSError *) = ^(NSError *error) {

UIAlertView * alert = [[UIAlertView alloc]

initWithTitle:@"Error"

message:[NSString stringWithFormat:@"Error: %@", [error description]]

delegate:nil

cancelButtonTitle:@"Ok"

otherButtonTitles:nil];

[alert show];

[alert release];

NSLog(@"A problem occured %@", [error description]);

};

ALAssetsLibrary *library = [[ALAssetsLibrary alloc]  init];

[library assetForURL:assetUrl resultBlock:assetRseult  failureBlock:failureBlock];

[library release];

resultBlock也可以如此定义:

ALAssetsLibraryAssetForURLResultBlock  assetsResultBlock = ^(ALAsset *result)

{

if (result == nil)

{

return;

}

UIImage *image = [UIImage imageWithCGImage:[[result  defaultRepresentation]fullScreenImage]];

[self.images addObject:image];

[self.assetArrays addObject:result];

};

获取照片的时间:

-(void)getDate:(ALAsset*)rule

{

NSDictionary *dic = [[rule  defaultRepresentation]metadata];

id dateTime = [[dic  objectForKey:@"{TIFF}"]objectForKey:@"DateTime"];

if (dateTime!=nil) {

NSArray *time = [dateTime

componentsSeparatedByCharactersInSet:[NSCharacterSet

whitespaceCharacterSet]];

NSString *dataStr = [time objectAtIndex:0];

if (![date containsObject:dataStr]) {

[date addObject:[time objectAtIndex:0]];

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值