1、压缩:
// 第一种
- (UIImage *)scaleImage:(UIImage *)image size:(CGSize)imageSize {
UIGraphicsBeginImageContext(imageSize);
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
// 第二种
- (void)imageDataLoad{
NSData *pngData = UIImagePNGRepresentation(self.pngImage);
[self.pngImageview setImage:[UIImage imageWithData:pngData]];
}
2、图片过滤
- (UIImage *)filter{
// 获取各种参数
CGImageRef imageRef = self.pngImage.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
size_t bits = CGImageGetBitsPerComponent(imageRef);
size_t bitsPreRow = CGImageGetBytesPerRow(imageRef);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
int alphaInfo = CGImageGetAlphaInfo(imageRef);
CGDataProviderRef provideRef = CGImageGetDataProvider(imageRef);
CFDataRef dataRef = CGDataProviderCopyData(provideRef);
int length = (int)CFDataGetLength(dataRef);
UInt8 *pixelBuf = CFDataGetMutableBytePtr((CFMutableDataRef)dataRef);
for (int i = 0; i < length; i+=4) {
[self yjhImageFilterFormBuf:pixelBuf offset:i];
}
CGContextRef contextRef = CGBitmapContextCreate(pixelBuf, width, height, bits, bitsPreRow, colorSpace, alphaInfo);
CGImageRef backImageRef = CGBitmapContextCreateImage(contextRef);
UIImage *backImage = [UIImage imageWithCGImage:backImageRef scale:[UIScreen mainScreen].scale orientation:self.pngImage.imageOrientation];
CFRelease(dataRef);
CFRelease(contextRef);
CFRelease(backImageRef);
return backImage;
}
// 像素点处理
- (void)yjhImageFilterFormBuf:(UInt8*)pixeBuf offset:(int)offset{
int offsetR = offset;
int offsetG = offset + 1;
int offsetB = offset + 2;
int red = pixeBuf[offsetR];
int green = pixeBuf[offsetG];
int blue = pixeBuf[offsetB];
pixeBuf[offsetB] = red * 0.3;
pixeBuf[offsetG] = green * 0.3;
pixeBuf[offsetB] = blue * 0.3;
}
3、 截屏
// 图片放到view
- (UIImage *)imageFromView:(UIView *)view {
UIGraphicsBeginImageContext(view.frame.size);
// 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 渲染到view
[view.layer renderInContext:context];
// 把上下文生成图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
newImage = [self getSubImage:CGRectMake(100, 100, 100, 100) image:newImage.CGImage];
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage *)getSubImage:(CGRect)rect image:(CGImageRef)cgImage {
CGImageRef imageRef = CGImageCreateWithImageInRect(cgImage, rect);
UIImage *subImage = [UIImage imageWithCGImage:imageRef];
return subImage;
}
// 传入一张图片进行裁剪
- (UIImage *)circleImage:(UIImage *)image {
// 图片编辑开始
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
// 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextAddEllipseInRect(context, CGRectMake(0, 0, 200, 200));
// 裁剪
CGContextClip(context);
[image drawInRect:CGRectMake(0, 0, 200, 200)];
// 图片编辑结束
UIGraphicsEndImageContext();
return image;
}