iOS开发中,UIImage相关的方法

1、根据UIView对象生成UIImage对象

/**
 根据UIView生成相应的UIImage对象,生成的UIImage对象的size即为view的size大小,方向为UIImageOrientationUp,即默认方向

 @param view 视图
 @return UIImage对象
 */
+ (UIImage *)imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

2、生成纯色的UIImage对象,即根据颜色生成UIImage

/**
 根据颜色和size大小生成UIImage对象,生成的UIImage对象大小即为参数imageSize的大小,颜色即为参数color的颜色,方向为UIImageOrientationUp,即默认方向

 @param color 颜色
 @param imageSize 大小
 @return UIImage对象
 */
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)imageSize
{
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

3、对UIImage对象进行剪裁操作

/**
 剪裁图片到指定的rect,rect决定了剪裁图片的左上角的坐标,和剪裁的大小

 @param rect 坐标大小
 @return UIImage对象
 */
- (UIImage *)imageCropToRect:(CGRect)rect
{
    CGRect drawRect = CGRectMake(-rect.origin.x , -rect.origin.y, self.size.width * self.scale, self.size.height * self.scale);
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
    
    [self drawInRect:drawRect];
    
    UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return tempImage;
}
//此方法与上面的方法效果一样,只不过使用的是相对比较底层的框架,是CGImage的范畴,但是论结果应该是一样的
//有不同的地方是,当你处理图片设计方向问题的时候,这个C语言的处理不是很友好,个人理解的是,CGImage这个框架不涉及方向的处理,所以这个方法还是慎重考虑使用。
- (UIImage *)imageCropToRect:(CGRect)rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height));
    UIImage *tempImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return tempImage;
}

4、对UIImage对象进行缩放操作,即按比例缩放到指定的大小

/**
 缩放图片大小到指定size

 @param size 大小
 @return UIImage对象
 */
- (UIImage*)imageScaleToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0 ,0 ,size.width ,size.height)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

5、修正图片的方向

/* 修正图片方向(应该只有在拍照完成使用照片的时候会用到) */
- (UIImage *)imageFixOrientation
{
    // No-op if the orientation is already correct
    if (self.imageOrientation == UIImageOrientationUp) return self;
    
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (self.imageOrientation) {
            case UIImageOrientationDown:
            case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
            case UIImageOrientationLeft:
            case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
            case UIImageOrientationRight:
            case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
            case UIImageOrientationUp:
            case UIImageOrientationUpMirrored:
            break;
    }
    
    switch (self.imageOrientation) {
            case UIImageOrientationUpMirrored:
            case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
            case UIImageOrientationLeftMirrored:
            case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            case UIImageOrientationUp:
            case UIImageOrientationDown:
            case UIImageOrientationLeft:
            case UIImageOrientationRight:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                             CGImageGetBitsPerComponent(self.CGImage), 0,
                                             CGImageGetColorSpace(self.CGImage),
                                             CGImageGetBitmapInfo(self.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (self.imageOrientation) {
            case UIImageOrientationLeft:
            case UIImageOrientationLeftMirrored:
            case UIImageOrientationRight:
            case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

6、图片质量的压缩

//通常我们使用下面的方法对图片进行压缩处理
//但是对于下面这个系统的方法开说,压缩系数需要一个合适的值,我当时记得是各种测试测出来一个比较合适的值
//不知道大家对于这个压缩系数有什么理解,还请指点一二
/**
 图片压缩

 @param image UIImage图片对象
 @return compressionQuality 压缩系数0-1之间的数值
 */
UIImageJPEGRepresentation(image, compressionQuality);

推荐阅读更多精彩内容