标签:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:_item.image0]; 5 6 UIImageView *imageView = [[UIImageView alloc] init]; 7 _imageView = imageView; 8 [imageView sd_setImageWithURL:[NSURL URLWithString:_item.image1] placeholderImage:image]; 9 CGFloat h = XMGScreenW / _item.width * _item.height; 10 imageView.frame = CGRectMake(0, 0, XMGScreenW, h); 11 [_scroolView addSubview:imageView]; 12 13 if (_item.is_bigPicture) { 14 _scroolView.contentSize = CGSizeMake(0, h); 15 16 if (_item.height > h) { 17 CGFloat scale = _item.height / h; 18 //2.设置缩放比例 19 _scroolView.maximumZoomScale = scale; 20 _scroolView.minimumZoomScale = 1; 21 } 22 } else { 23 imageView.center = CGPointMake(XMGScreenW * 0.5, XMGScreenH * 0.5); 24 } 25 // 缩放 26 //1.设置代理,告诉它哪个View需要做缩放 27 //2.设置缩放比例 28 _scroolView.delegate = self; 29 } 30 #pragma mark - UIScrollViewDelegate 31 // 作用:返回需要做缩放的view 32 // 调用:每次缩放的时候才会调用 33 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 34 { 35 return _imageView; 36 }
1 // 点击保存调用 2 - (IBAction)save:(id)sender { 3 // 保存系统相册 4 UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 5 } 6 // 询问下用户是否允许当前app访问相册 7 // 监听图片是否保存完成 8 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 9 { 10 if (error) { 11 [SVProgressHUD showErrorWithStatus:@"保存失败"]; 12 } else { 13 [SVProgressHUD showSuccessWithStatus:@"保存成功"]; 14 } 15 }
1 // 点击保存调用 2 - (IBAction)save:(id)sender { 3 //查看当前app授权状态 4 PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; 5 switch (status) { 6 case PHAuthorizationStatusNotDetermined: 7 { // 未授权,弹出授权框 8 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { 9 // 用户选择完毕就会调用—选择允许,直接保存 10 if (status == PHAuthorizationStatusAuthorized) { 11 [self savePhoto]; 12 } 13 }]; 14 选择不允许访问,就不保存 15 break; 16 } 17 case PHAuthorizationStatusAuthorized: 18 { // 授权,就直接保存 19 [self savePhoto]; 20 break; 21 } 22 default: 23 {// 拒绝 告知用户去哪打开授权 24 [SVProgressHUD showInfoWithStatus:@"打开设置 -> 查找百思不得姐 -> 打开照片开关 -> 允许当前app访问系统相册就可以保存图片"]; 25 break; 26 } 27 } 28 } 29 30 // 添加图片到自己相册 31 - (void)savePhoto 32 { 33 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 34 // 1.创建图片请求类(创建系统相册中新的图片)PHAssetCreationRequest 35 // 把图片放在系统相册 36 PHAssetCreationRequest *assetCreationRequest = [PHAssetCreationRequest creationRequestForAssetFromImage:_imageView.image]; 37 38 // 2.创建相册请求类(修改相册)PHAssetCollectionChangeRequest 39 PHAssetCollectionChangeRequest *assetCollectionChangeRequest = nil; 40 41 // 获取之前相册 42 PHAssetCollection *assetCollection = [self fetchAssetCollection:@"百思不得姐"]; 43 44 // 判断是否已有相册 45 if (assetCollection) { 46 // 如果存在已有同名相册 指定这个相册,创建相册请求修改类 47 assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection]; 48 } else { //不存在,创建新的相册 49 assetCollectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"百思不得姐"]; 50 } 51 // 3.把图片添加到相册中 52 // NSFastEnumeration:以后只要看到这个,就可以表示数组 53 //assetCreationRequest.placeholderForCreatedAsset 图片请求类占位符(相当于一个内存地址) 54 //因为creationRequestForAssetFromImage方法是异步实行的,在这里不能保证 assetCreationRequest有值 55 56 [assetCollectionChangeRequest addAssets:@[assetCreationRequest.placeholderForCreatedAsset]]; 57 58 } completionHandler:^(BOOL success, NSError * _Nullable error) { 59 60 if (success) { 61 [SVProgressHUD showSuccessWithStatus:@"保存成功"]; 62 } else { 63 [SVProgressHUD showErrorWithStatus:@"保存失败"]; 64 } 65 66 }]; 67 } 68 69 // 指定相册名称,获取相册 70 - (PHAssetCollection *)fetchAssetCollection:(NSString *)title 71 { 72 // 获取相簿中所有自定义相册 73 PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; 74 遍历相册,判断是否存在同名的相册 75 for (PHAssetCollection *assetCollection in result) { 76 if ([title isEqualToString:assetCollection.localizedTitle]) { 存在,就返回这个相册 77 return assetCollection; 78 } 79 } 80 return nil; 81 }
标签:
原文地址:http://www.cnblogs.com/xiaotian666/p/5762326.html