在我们开发过程中,经常会遇到对UIImage的处理,那么在这里简单介绍几种常用的吧,我一般把它们统一写在UIImage的Category中
首先是对图片的压缩处理
- (UIImage *)scaledImageWithSize:(CGSize)size { UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
然后是拍照后图片翻转的处理
- (UIImage *)fixOrientation:(UIImage *)aImage { //纠正图片 if (aImage.imageOrientation == UIImageOrientationUp) return aImage; CGAffineTransform transform = CGAffineTransformIdentity; switch (aImage.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, aImage.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; default: break; } switch (aImage.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); break; default: break; } CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height, CGImageGetBitsPerComponent(aImage.CGImage), 0, CGImageGetColorSpace(aImage.CGImage), CGImageGetBitmapInfo(aImage.CGImage)); CGContextConcatCTM(ctx, transform); switch (aImage.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage); break; } CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img; }
以及截取某图片上面的部分区域
- (CGImageRef)imageRefCutFromImage:(UIImage *)image { CGFloat scaleImg = image.size.width/image.size.height; CGRect rect = CGRectMake(0, 0, image.size.width*image.scale, image.size.width*image.scale); if (scaleImg > 1) { CGFloat originX = (image.size.width*image.scale - image.size.height * image.scale)/2; rect = CGRectMake(originX, 0, image.size.height * image.scale, image.size.height * image.scale); }else { rect = CGRectMake(0, 0, image.size.width*image.scale, image.size.width*image.scale); } CGImageRef cgimg = CGImageCreateWithImageInRect([image CGImage], rect); return cgimg; }
就写这么多吧,等以后遇到新问题,再来继续解释
时间: 2024-10-12 16:46:44