iOS图片裁剪、绘制背景色

- (UIImage *)croppedImage:(UIImage *)image

{
    if (image)
    {
        float min = MIN(image.size.width,image.size.height);
        CGRect rectMAX = CGRectMake((image.size.width-min)/2, (image.size.height-min)/2, min, min);
       
        CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rectMAX);
       
        UIGraphicsBeginImageContext(rectMAX.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, CGRectMake(0, 0, min, min), subImageRef);
        UIImage *viewImage = [UIImage imageWithCGImage:subImageRef];
        UIGraphicsEndImageContext();
        CGImageRelease(subImageRef);
        return viewImage;
    }
   
    return nil;

}

2 iOS绘制背景色

CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor *bgColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1.0];
    CGContextSetStrokeColorWithColor(context, bgColor.CGColor);
    CGContextSetFillColorWithColor(context, bgColor.CGColor);
    CGRect bgRect = CGRectMake(0, kUserAlbumWidth, self.frame.size.width, kThumbWidth);
    CGContextAddRect(context, bgRect);
    CGContextDrawPath(context, kCGPathFillStroke);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ada3a859ab2bdf6716f8738ed33f5b69.html