需要先导入文件
#import <AssetsLibrary/AssetsLibrary.h>
从相册取得图片信息
// ALAssetsLibrary 销毁了为nil时;下面的group(ALAssetsGroup)、result(ALAsset)就没有了;ALAssetsLibrary可以设置为全局变量就可以在这个文件中取得它的信息,不能传递到其他文件,相=想传到其他文件就要把你想要的信息取出来
ALAssetsLibrary *library =[[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//其中一条group信息
//ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:82
/** ALAssetsGroup 根据[group valueForProperty:<#(NSString *)#>]取得属性
*ALAssetsGroupPropertyName 这个组的名字
*ALAssetsGroupPropertyType 类型
*ALAssetsGroupPropertyPersistentID 相册的ID,也就是相册的文件名
*ALAssetsGroupPropertyURL 地址
*/
// [[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"Camera Roll"]
//得到这个group所有的图片
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
PhotoModel *model = [[PhotoModel alloc]init];
model.date= [sset valueForProperty:ALAssetPropertyDate];//储存日期
model.type = [sset valueForProperty:ALAssetPropertyType];//文件类型
model.url =[sset defaultRepresentation].url;//地址
//用UIimage传递图片,cgimageref 会照成内存问题(没有深入解决这个问题?)
model.thumbNailimage =[UIImage imageWithCGImage:[sset thumbnail]];//缩略图
model.fullScreenImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];//高清图
}
} failureBlock:^(NSError *error) {
NSLog(@"error:%@",error.localizedDescription);
}];
拍照以后把图片保存到相册
#pragma mark--uiimagepickercontroller delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
/**保存图片到相册*/
[assetslibrary writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {
/**根据assetURL区得在相册中的信息asset*/
[assetslibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
NSString *date =[asset valueForProperty:ALAssetPropertyDate];
PhotoModel *model = [[PhotoModel alloc]init];
model.date= date;
model.type = [asset valueForProperty:ALAssetPropertyType];
model.url =[asset defaultRepresentation].url;
model.thumbNailimage =[UIImage imageWithCGImage:[sset thumbnail]];
model.fullScreenImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
[self.dataArray addObject:model];
} failureBlock:^(NSError *error) {
}];
}];
}
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
为了相册选择图片、拍照 让它们的图片信息一致。
拍照时想要设置为中文按钮需要把info.plist的Localization native development region 设置为China