pragma mark - 通过URL生成二维码
- (UIImage *) create2DCodeImageWithSize:(CGFloat) size codeUrl:(NSString *) codeUrl{
// 实例化二维码滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
[filter setDefaults];
// 将字符串转换成NSdata
NSData *data = [codeUrl dataUsingEncoding:NSUTF8StringEncoding];
// 通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
[filter setValue:data forKey:@"inputMessage"];
// 生成二维码
CIImage *outputImage = [filter outputImage];
UIImage *image = [self changeImageSizeWithCIImage:outputImage andSize:size];
return image;
}
pragma mark - 调整图片大小
- (UIImage *)changeImageSizeWithCIImage:(CIImage *)ciImage andSize:(CGFloat)size{
CGRect extent = CGRectIntegral(ciImage.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 创建bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:ciImage fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
pragma mark - 绘制并保存图片
- (void) clickDownloadButton {
UIGraphicsBeginImageContext(CGSizeMake(640, 910));
UIImage *backImage = [UIImage imageNamed:@"download_pic"];
[backImage drawInRect:CGRectMake(0, 0, 640, 910)];
UIImage *imageface = [self create2DCodeImageWithSize:370 codeUrl:_codeUrl];
[imageface drawInRect:CGRectMake(135, 270, 370, 370)];
UIImage *iconImage = [self makeRoundedImage:_userIconImageView.image radius:50];
[iconImage drawInRect:CGRectMake(70, 126, 100, 100)];
NSString *str1 = _userNameLabel.text;
[str1 drawAtPoint:CGPointMake(200, 136) withAttributes:@{NSForegroundColorAttributeName:RGBCOLOR(37, 182, 237),NSFontAttributeName:[UIFont systemFontOfSize:32]}];
NSString *str2 = _userShopLabel.text;
[str2 drawAtPoint:CGPointMake(200, 186) withAttributes:@{NSForegroundColorAttributeName:RGBCOLOR(69, 69, 69),NSFontAttributeName:[UIFont systemFontOfSize:28]}];
//从当前上下文获取图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//结束图像绘制上下文
UIGraphicsEndImageContext();
//保存到相册
// UIImageWriteToSavedPhotosAlbum(image, self, @selector(bigPicImage:didFinishSavingWithError:contextInfo:), nil);
[self saveImage:image];
}
pragma mark - UIImage切圆角
-(UIImage *) makeRoundedImage:(UIImage *) image
radius: (float) radius;
{
// 截取一部分图片
CGRect rect = CGRectMake( 0, 0, MIN(image.size.width, image.size.height), MIN(image.size.width, image.size.height));
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *bg = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, 2 * radius, 2 * radius);
imageLayer.contentsScale = [[UIScreen mainScreen] scale];
imageLayer.contents = (id) bg.CGImage;
imageLayer.cornerRadius = radius;
imageLayer.masksToBounds = YES;
UIGraphicsBeginImageContext(CGSizeMake(2 * radius, 2 * radius));
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}
- (PHAssetCollection *)collection{
// 先获得之前创建过的相册
PHFetchResult <PHAssetCollection *> *collectionResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *collection in collectionResult) {
if ([collection.localizedTitle isEqualToString:photosName]) {
return collection;
}
}
// 如果相册不存在,就创建新的相册(文件夹)
__block NSString *collectionId = nil; // __block修改block外部的变量的值
// 这个方法会在相册创建完毕后才会返回
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
// 新建一个PHAssertCollectionChangeRequest对象, 用来创建一个新的相册
collectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:photosName].placeholderForCreatedAssetCollection.localIdentifier;
} error:nil];
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionId] options:nil].firstObject;
}
/**
* 返回相册,避免重复创建相册引起不必要的错误
*/
- (void)saveImage:(UIImage *) image{
/*
PHAsset : 一个PHAsset对象就代表一个资源文件,比如一张图片
PHAssetCollection : 一个PHAssetCollection对象就代表一个相册
*/
__block NSString *assetId = nil;
// 1. 存储图片到"相机胶卷"
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ // 这个block里保存一些"修改"性质的代码
// 新建一个PHAssetCreationRequest对象, 保存图片到"相机胶卷"
// 返回PHAsset(图片)的字符串标识
assetId = [PHAssetCreationRequest creationRequestForAssetFromImage:image].placeholderForCreatedAsset.localIdentifier;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
NSLog(@"保存图片到相机胶卷中失败");
return;
}
NSLog(@"成功保存图片到相机胶卷中");
// 2. 获得相册对象
PHAssetCollection *collection = [self collection];
// 3. 将“相机胶卷”中的图片添加到新的相册
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
// 根据唯一标示获得相片对象
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
// 添加图片到相册中
[request addAssets:@[asset]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[MBProgressHUD ZP_ShowRemindUserText:@"保存到相册失败" toView:self.superview inPeriod:1 yOffset:200];
}];
return;
}
// PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
// [PHAssetCreationRequest deleteAssets:@[asset]];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[MBProgressHUD ZP_ShowRemindUserText:@"图片已保存到相册/掌上好房通" toView:self.superview inPeriod:1 yOffset:200];
}];
}];
}];
}