在我们开发过程中,经常会遇到对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.p_w_picpathOrientation == UIImageOrientationUp)
        return aImage;
    
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (aImage.p_w_picpathOrientation) {
        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.p_w_picpathOrientation) {
        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.p_w_picpathOrientation) {
        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 p_w_picpathWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    
    return img;
}


以及截取某图片上面的部分区域


 
- (CGImageRef)p_w_picpathRefCutFromImage:(UIImage *)p_w_picpath {
    CGFloat scaleImg = p_w_picpath.size.width/p_w_picpath.size.height;
    CGRect rect = CGRectMake(0, 0, p_w_picpath.size.width*p_w_picpath.scale, p_w_picpath.size.width*p_w_picpath.scale);
    if (scaleImg > 1) {
        CGFloat originX = (p_w_picpath.size.width*p_w_picpath.scale - p_w_picpath.size.height * p_w_picpath.scale)/2;
        rect = CGRectMake(originX, 0, p_w_picpath.size.height * p_w_picpath.scale, p_w_picpath.size.height * p_w_picpath.scale);
    }else
    {
        rect = CGRectMake(0, 0, p_w_picpath.size.width*p_w_picpath.scale, p_w_picpath.size.width*p_w_picpath.scale);
    }
    CGImageRef cgimg = CGImageCreateWithImageInRect([p_w_picpath CGImage], rect);
    
    return cgimg;
}


就写这么多吧,等以后遇到新问题,再来继续解释