参考:https://www.jianshu.com/p/0bd04e3fcb7c
一、图片缩放
二、设置图片黑白色
方式一:用GPUImage ,需要构建GPUImage到工程中。
/**
图像黑白,像黑白漫画
*/
- (void)setupImageViewBlackAndWhiteWithImage:(UIImage *)image {
GPUImagePicture *imagePicture = [[GPUImagePicture alloc] initWithImage:image];
GPUImageAverageLuminanceThresholdFilter *averageFliter = [[GPUImageAverageLuminanceThresholdFilter alloc] init];
averageFliter.thresholdMultiplier = 1.0;
[imagePicture addTarget:averageFliter];
[averageFliter useNextFrameForImageCapture];
[imagePicture processImage];
_imageView.image = [averageFliter imageFromCurrentFramebuffer];
}
方式二:使用CIImage
/**
设置黑白色
*/
+ (UIImage *)blackAndWhite:(UIImage *)image
{
CIImage *beginImage = [CIImage imageWithCGImage:image.CGImage];
CIImage *blackAndWhite = [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, beginImage, @"inputBrightness", @0.0, @"inputContrast", @2.5, @"inputSaturation", @0.0, nil].outputImage;
CIImage *output = [CIFilter filterWithName:@"CIExposureAdjust" keysAndValues:kCIInputImageKey, blackAndWhite, @"inputEV", @1.0, nil].outputImage;
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];
UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:image.scale orientation:image.imageOrientation];
CGImageRelease(cgiimage);
return newImage;
}
三、图片按最大或者最小边缩放图片
//指定缩减的大小
+ (UIImage *)scaleToSize:(CGSize)size image:(UIImage *)image max:(BOOL)max {
CGSize originImageSize = image.size;
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
CGFloat ratio = max ? MAX(size.width / originImageSize.width, size.height / originImageSize.height) : MIN(size.width / originImageSize.width, size.height / originImageSize.height);
UIGraphicsBeginImageContext(CGSizeMake(originImageSize.width * ratio, originImageSize.height * ratio));
// 绘制改变大小的图片
[image drawInRect:CGRectMake(0,0, originImageSize.width * ratio, originImageSize.height * ratio)];
// 从当前context中创建一个改变大小后的图片
UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
//返回新的改变大小后的图片
return scaledImage;
}