iOS ALAssetsLibrary-代码操作iOS相册资源
2014-09-07 22:26
288 查看
在iOS中,我们调用摄像头和选择相册中的资源,我们可以使用:UIImagePickerController类来完成。
当然,我们也可以不使用UI的形式来访问iOS设备的相册资源。
那就是使用:ALAssetsLibrary
一、ALAssetsLibrary是什么
可以说,是一个桥梁把。连接了我们应用程序和相册之间的访问。
ALAssetsLibrary提供了我们对iOS设备中的相片、视频的访问。
ALAssetsLibrary被封装在 框架中。所以,我们在使用时,需要引入该框架。
贴:
self.view.backgroundColor =
[UIColor whiteColor];
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
dispatch_queue_t dispatchQueue
=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchQueue, ^(void) {
// 遍历所有相册
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL*stop)
{
// 遍历每个相册中的项ALAsset
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index,BOOL *stop)
{
__block BOOL foundThePhoto
=NO;
if (foundThePhoto){
*stop = YES;
}
//
ALAsset的类型
NSString *assetType
= [resultvalueForProperty:ALAssetPropertyType];
if ([assetTypeisEqualToString:ALAssetTypePhoto]){
foundThePhoto = YES;
*stop = YES;
ALAssetRepresentation*assetRepresentation
=[result defaultRepresentation];
CGFloat imageScale
= [assetRepresentation scale];
UIImageOrientationimageOrientation
= (UIImageOrientation)[assetRepresentation orientation];
dispatch_async(dispatch_get_main_queue(),
^(void) {
CGImageRefimageReference
= [assetRepresentation fullResolutionImage];
// 对找到的图片进行操作
UIImage *image
=[[UIImage alloc] initWithCGImage:imageReference scale:imageScaleorientation:imageOrientation];
if (image
!= nil){
self.imageView =
[[UIImageView alloc] initWithFrame:self.view.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image =
image;
[self.viewaddSubview:self.imageView];
} else {
NSLog(@"Failed
to create the image.");
} });
}
}];
}
failureBlock:^(NSError *error)
{
NSLog(@"Failed
to enumerate the asset groups.");
}];
});
乱啊,没办法,在xCode中是OK的。
需要解释的几点:
1.流程:该代码的流程,就是先遍历所有相册,然后,遍历每个相册中的第一张图片。
2.ALAssetsGroup:指代一个相册。
3.ALAsset:每一个ALAsset代表一个单一资源文件(也就是一张图片,或者一个视频文件)
4.ALAssetRepresentation:ALAssetRepresentation封装了ALAsset,包含了一个资源文件中的很多属性。(可以说是ALAsset的不同的表示方式,本质上都表示同一个资源文件)
希望对你有所帮助!
当然,我们也可以不使用UI的形式来访问iOS设备的相册资源。
那就是使用:ALAssetsLibrary
一、ALAssetsLibrary是什么
可以说,是一个桥梁把。连接了我们应用程序和相册之间的访问。
ALAssetsLibrary提供了我们对iOS设备中的相片、视频的访问。
ALAssetsLibrary被封装在 框架中。所以,我们在使用时,需要引入该框架。
贴:
self.view.backgroundColor =
[UIColor whiteColor];
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
dispatch_queue_t dispatchQueue
=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchQueue, ^(void) {
// 遍历所有相册
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL*stop)
{
// 遍历每个相册中的项ALAsset
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index,BOOL *stop)
{
__block BOOL foundThePhoto
=NO;
if (foundThePhoto){
*stop = YES;
}
//
ALAsset的类型
NSString *assetType
= [resultvalueForProperty:ALAssetPropertyType];
if ([assetTypeisEqualToString:ALAssetTypePhoto]){
foundThePhoto = YES;
*stop = YES;
ALAssetRepresentation*assetRepresentation
=[result defaultRepresentation];
CGFloat imageScale
= [assetRepresentation scale];
UIImageOrientationimageOrientation
= (UIImageOrientation)[assetRepresentation orientation];
dispatch_async(dispatch_get_main_queue(),
^(void) {
CGImageRefimageReference
= [assetRepresentation fullResolutionImage];
// 对找到的图片进行操作
UIImage *image
=[[UIImage alloc] initWithCGImage:imageReference scale:imageScaleorientation:imageOrientation];
if (image
!= nil){
self.imageView =
[[UIImageView alloc] initWithFrame:self.view.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image =
image;
[self.viewaddSubview:self.imageView];
} else {
NSLog(@"Failed
to create the image.");
} });
}
}];
}
failureBlock:^(NSError *error)
{
NSLog(@"Failed
to enumerate the asset groups.");
}];
});
乱啊,没办法,在xCode中是OK的。
需要解释的几点:
1.流程:该代码的流程,就是先遍历所有相册,然后,遍历每个相册中的第一张图片。
2.ALAssetsGroup:指代一个相册。
3.ALAsset:每一个ALAsset代表一个单一资源文件(也就是一张图片,或者一个视频文件)
4.ALAssetRepresentation:ALAssetRepresentation封装了ALAsset,包含了一个资源文件中的很多属性。(可以说是ALAsset的不同的表示方式,本质上都表示同一个资源文件)
希望对你有所帮助!
相关文章推荐
- ALAssetsLibrary-代码操作iOS相册资源
- ALAssetsLibrary-代码操作iOS相册资源
- ALAssetsLibrary-代码操作iOS相册资源
- ALAssetsLibrary-代码操作iOS相册资源
- ALAssetsLibrary-代码操作iOS相册资源
- ios 相册操作 ALAssetsLibrary
- iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”
- iOS相册访问 ALAssetsLibrary(获取到相册的图片)+ 图片格式
- IOS APP 国际化(实现不跟随系统语言,不用重启应用,代码切换stroyboard ,xib ,图片,其他资源)
- iOS ALAssetsLibrary获取所有图片
- ios操作文件和字符串的示例代码
- IOS学习笔记22—文件操作(NSFileManager)结合相册小例子
- iOS ALAssetsLibrary 无法取得照片流中的图片
- ALAssetsLibrary获取相册列表
- (原) ALAssetsLibrary遍历相册及略所图
- ios多线程操作(七)—— GCD延迟操作与一次性代码
- 关于cocos2dx代码,资源移植到ios上的问题
- IOS学习笔记22—文件操作(NSFileManager)结合相册小例子
- iOS文件操作(NSFileManager)结合相册小例子