1. 图片压缩
1.1 直接格式转换(jpg(三个颜色通道,rgb) png(四个颜色通道,rgba) (jpg更轻量)
NSData *pngData = UIImagePNGRepresentation(_albumImage);
NSData *jpgData = UIImageJPEGRepresentation(_albumImage, scale);
将图片压缩到小于指定大小(通常限制在36kb以下)
//利用遍历压缩
while (jpgData.length > 36*1024) {
scale = -0.1;
jpgData = UIImageJPEGRepresentation(_albumImage, scale);
if (scale <= 0.1) {
break;
}
}
1.2 context上修改
// 通过上下文对图片压缩处理
- (UIImage*)scaleImage:(UIImage*)image size:(CGSize)imageSize{
UIGraphicsBeginImageContext(imageSize);
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
2. 图片处理
2.1 图片像素上修改
位图的创建
//1.图片data 2.图片width 3.height
//4.位图的每一个组成部分多少大小 5.每一行多少个字节
//6.图片空间 7图片信息(图片的透明度 )
CGBitmapContextCreate(void * _Nullable data, size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow, CGColorSpaceRef _Nullable space, uint32_t bitmapInfo)
#注意的是:用core框架的时候要边写边注意释放!!!
- (UIImage *)cpcFilter{
//获取图片基本信息
CGImageRef imageRef = self.image.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
size_t bits = CGImageGetBitsPerComponent(imageRef);
size_t bitsPerRow = CGImageGetBytesPerRow(imageRef);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
int alphaInfo = CGImageGetAlphaInfo(imageRef);
//获得CFDataRef
CGDataProviderRef provideRef = CGImageGetDataProvider(imageRef);
CFDataRef dataRef = CGDataProviderCopyData(provideRef);
//原生的像素长度
int lenght = (int)CFDataGetLength(dataRef);
//转换成指针形式
UInt8 *pixelBuf = (UInt8 *)CFDataGetMutableBytePtr((CFMutableDataRef)dataRef);
for (int i = 0; i < lenght; i+=4) {//注意:每个像素有4个颜色通道 故i+=4 !!!
//////修改原始像素RGB数据
[self cpcImageFilterFormBuf:pixelBuf offset:i];
}
CGContextRef contextRef = CGBitmapContextCreate(pixelBuf, width, height, bits, bitsPerRow, colorSpace, alphaInfo);
//转换uiimage
CGImageRef backImageRef = CGBitmapContextCreateImage(contextRef);
UIImage *backImage = [UIImage imageWithCGImage:backImageRef scale:[UIScreen mainScreen].scale orientation:self.image.imageOrientation];
//记得要释放
CFRelease(dataRef);
CFRelease(contextRef);
CFRelease(backImageRef);
return backImage;
}
// 这块就是对每个像素点进行加工(可以根据自己需求进行图形运算,可以用第三方)
- (void)cpcImageFilterFormBuf:(UInt8*)pixelBuf offset:(int)offset{
int offsetR = offset;
int offsetG = offset + 1;
int offsetB = offset + 2;
int red = pixelBuf[offsetR];
int green = pixelBuf[offsetG];
int blue = pixelBuf[offsetB];
int gray = (red + green + blue)/3;
pixelBuf[offsetR] = gray;
pixelBuf[offsetG] = gray;
pixelBuf[offsetB] = gray;
}
2.2 context上修改
#圆形图片
- (UIImage*)circleImage:(UIImage*)image{
// 1 创建上下文
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
// 2 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 3 操作上下文
CGContextSetLineWidth(context, 1);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGRect rect = CGRectMake(0, 0, 200, 200);
CGContextAddEllipseInRect(context, rect);
// 截剪上下文为圆形 闭环的
CGContextClip(context);
// 4 把image 渲染到相应的上下文中
[image drawInRect:CGRectMake(0, 0, 200, 200)];
UIImage *backImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return backImage;
}
#截图
- (UIImage*)imageFromView:(UIView*)view{
// 1 创建上下文
UIGraphicsBeginImageContext(view.frame.size);
// 2 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 3 把view 渲染到上下吻
[view.layer renderInContext:context];
// 4 把上下文中生成图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
# 5 局部截图(本质是截图片的部分)
image = [self getSubImage:CGRectMake(100, 100, _eocImageV.frame.size.width, _eocImageV.frame.size.height) image:image.CGImage];
UIGraphicsEndImageContext();
return image;
}
- (UIImage*)getSubImage:(CGRect)rect image:(CGImageRef)cgImage{
CGImageRef imageRef = CGImageCreateWithImageInRect(cgImage, rect);
UIImage *subImage = [UIImage imageWithCGImage:imageRef];
return subImage;
}
#对图片颜色渲染
- (UIImage*)imageWithColor:(UIColor*)color{
UIImage *rendImage = [UIImage imageNamed:@"3.jpg"];
// 1 创建上下文
UIGraphicsBeginImageContext(rendImage.size);
// 2 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 3 把图片渲染到上下文
[rendImage drawInRect:CGRectMake(0, 0, rendImage.size.width, rendImage.size.height)];
UIColor *redColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
CGContextSetFillColorWithColor(context, redColor.CGColor);
// 3 把颜色渲染到上下文
CGContextSetBlendMode(context, kCGBlendModeNormal);
// 4 颜色渲染区域
CGContextFillRect(context, CGRectMake(0, 0,rendImage.size.width, rendImage.size.height));
// 5 生成图片
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *redImage = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);
UIGraphicsEndImageContext();
// 裁剪
return redImage;
}
3. 图片缩放
3.1 利用系统缩放
#放在scrollView上缩放 ,但不大满意
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"缩放";
_scrollview.maximumZoomScale = 5;
_scrollview.minimumZoomScale = 0.5;
}
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return _imageView;
}
3.2 自己实现
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"缩放";
[self cpcScale];
}
- (void)cpcScale{
//默认会覆盖掉scrollView的系统pinch
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchHandel:)];
[_scrollview addGestureRecognizer:pinchGesture];
}
- (void)pinchHandel:(UIPinchGestureRecognizer*)gesture{
NSLog(@"%f", gesture.scale);
//记得把scale置1
if (gesture.state == UIGestureRecognizerStateBegan) {
_preScal = 1;
}
float scaleF = gesture.scale - _preScal + 1;
_preScal = gesture.scale;
_imageView.transform = CGAffineTransformScale(_imageView.transform, scaleF, scaleF);
[_scrollview setContentSize:_imageView.frame.size];
}