相册

#import "AssetsLibrary/AssetsLibrary.h"


@interfaceBigPictureViewController()

/**图片*/

@property(nonatomic,weak)UIImageView*imageView;

/**相册库*/

@property(nonatomic,strong)ALAssetsLibrary*library;

@end

@implementationBigPictureViewController

- (ALAssetsLibrary*)library

{

if(!_library) {

_library= [[ALAssetsLibraryalloc]init];

}

return_library;

}

- (IBAction)back {

[selfdismissViewControllerAnimated:YEScompletion:nil];

}

- (void)viewDidLoad {

[superviewDidLoad];

//滚动控件

UIScrollView*scrollView = [[UIScrollViewalloc]init];

scrollView.frame= [UIScreenmainScreen].bounds;

scrollView.backgroundColor= [UIColorblackColor];

scrollView.delegate=self;

[self.viewinsertSubview:scrollViewatIndex:0];

//图片

UIImageView*imageView = [[UIImageViewalloc]init];

[imageViewsd_setImageWithURL:[NSURLURLWithString:self.topic.image1]];

[scrollViewaddSubview:imageView];

self.imageView= imageView;

//图片的尺寸

imageView.x=0;

imageView.width=XMGScreenW;

imageView.height=self.topic.height* imageView.width/self.topic.width;

if(imageView.height>XMGScreenH) {//图片过长

imageView.y=0;

scrollView.contentSize=CGSizeMake(0, imageView.height);

}else{//图片居中显示

imageView.centerY=XMGScreenH*0.5;

}

//伸缩

CGFloatmaxScale =self.topic.height/ imageView.height;

if(maxScale >1.0) {

scrollView.maximumZoomScale= maxScale;

}

}

staticNSString*constXMGGroupNameKey =@"GroupNameKey";

staticNSString*constXMGDefaultGroupName =@"XXXXXXXX";

- (NSString*)groupName

{

//先从沙盒中取得名字

NSString*groupName = [[NSUserDefaultsstandardUserDefaults]stringForKey:XMGGroupNameKey];

if(groupName ==nil) {//沙盒中没有存储任何文件夹的名字

groupName =XMGDefaultGroupName;

//存储名字到沙盒中

[[NSUserDefaultsstandardUserDefaults]setObject:groupNameforKey:XMGGroupNameKey];

[[NSUserDefaultsstandardUserDefaults]synchronize];

}

returngroupName;

}

- (IBAction)save

{

//获得文件夹的名字

__blockNSString*groupName = [selfgroupName];

// self的弱引用

XMGWeakSelf;

//图片库

__weakALAssetsLibrary*weakLibrary =self.library;

//创建文件夹

[weakLibraryaddAssetsGroupAlbumWithName:groupNameresultBlock:^(ALAssetsGroup*group) {

if(group) {//新创建的文件夹

//添加图片到文件夹中

[weakSelfaddImageToGroup:group];

}else{//文件夹已经存在

[weakLibraryenumerateGroupsWithTypes:ALAssetsGroupAllusingBlock:^(ALAssetsGroup*group,BOOL*stop) {

NSString*name = [groupvalueForProperty:ALAssetsGroupPropertyName];

if([nameisEqualToString:groupName]) {//是自己创建的文件夹

//添加图片到文件夹中

[weakSelfaddImageToGroup:group];

*stop =YES;//停止遍历

}elseif([nameisEqualToString:@"Camera Roll"]) {

//文件夹被用户强制删除了

groupName = [groupNamestringByAppendingString:@" "];

//存储新的名字

[[NSUserDefaultsstandardUserDefaults]setObject:groupNameforKey:XMGGroupNameKey];

[[NSUserDefaultsstandardUserDefaults]synchronize];

//创建新的文件夹

[weakLibraryaddAssetsGroupAlbumWithName:groupNameresultBlock:^(ALAssetsGroup*group) {

//添加图片到文件夹中

[weakSelfaddImageToGroup:group];

}failureBlock:nil];

}

}failureBlock:nil];

}

}failureBlock:nil];

}

/**

*添加一张图片到某个文件夹中

*/

- (void)addImageToGroup:(ALAssetsGroup*)group

{

__weakALAssetsLibrary*weakLibrary =self.library;

//需要保存的图片

CGImageRefimage =self.imageView.image.CGImage;

//添加图片到【相机胶卷】

[weakLibrarywriteImageToSavedPhotosAlbum:imagemetadata:nilcompletionBlock:^(NSURL*assetURL,NSError*error) {

[weakLibraryassetForURL:assetURLresultBlock:^(ALAsset*asset) {

//添加一张图片到自定义的文件夹中

[groupaddAsset:asset];

[SVProgressHUDshowSuccessWithStatus:@"保存成功!"];

}failureBlock:nil];

}];

}

- (void)getAllPhotos

{

ALAssetsLibrary*library = [[ALAssetsLibraryalloc]init];

//遍历所有的文件夹,一个ALAssetsGroup对象就代表一个文件夹

[libraryenumerateGroupsWithTypes:ALAssetsGroupAllusingBlock:^(ALAssetsGroup*group,BOOL*stop) {

//遍历文件夹内的所有多媒体文件(图片、视频),一个ALAsset对象就代表一张图片

[groupenumerateAssetsUsingBlock:^(ALAsset*result,NSUIntegerindex,BOOL*stop) {

//缩略图

XMGLog(@"%@", [UIImage imageWithCGImage:result.thumbnail]);

//获得原始图片

//XMGLog(@"%@", [UIImage imageWithCGImage:result.defaultRepresentation.fullResolutionImage]);

}];

}failureBlock:nil];

}

//自定义照相机: AVCaptureSession

//通过拍照获得一张图片

//UIImagePickerController *picker = [[UIImagePickerController alloc] init];

//picker.sourceType = UIImagePickerControllerSourceTypeCamera;

//[self presentViewController:picker animated:YES completion:nil];

//从相册中挑选一张图片

//UIImagePickerController *picker = [[UIImagePickerController alloc] init];

//picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

//[self presentViewController:picker animated:YES completion:nil];

#pragma mark -

- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView

{

returnself.imageView;

}

@end

你可能感兴趣的:(相册)