UIImage的截取

原创
2016/09/07 08:49
阅读数 104

本文参考 http://www.cnblogs.com/smileEvday/archive/2013/05/25/IOSImageEdit.html

在开发过程中有需要做图片处理

1、从摄像头获取UIImage,方向旋转问题解决方法

/**
 *  1、利用了UIImage中的drawInRect方法,它会将图像绘制到画布上,并且已经考虑好了图像的方向
 *  2、根据imageOrientation做相应的转换
 */
- (UIImage *)normalImage {

    if (self.imageOrientation == UIImageOrientationUp) return self;

    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    [self drawInRect:(CGRect){0, 0, self.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return normalizedImage;
}

2、指定范围及缩放比率截取图片

- (UIImage *)imageForCropRect:(CGRect)cropRect scale:(CGFloat)scale {
    
    //定义myImageRect,截图的区域
    CGImageRef imageRef = self.CGImage;
    CGImageRef cropImageRef = CGImageCreateWithImageInRect(imageRef, cropRect);
    CGImageRelease(imageRef);
    
    CGSize size = cropRect.size;
    if (scale > 0) {
        size = CGSizeMake(size.width*scale, size.height*scale);
    }
    
    //绘制图片
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), cropImageRef);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(cropImageRef);
    
    return image;
}

3、指定多个点,在图片中抠图

/*
 *  截取不规则图形
 */
- (UIImage*)imageForCropPaths:(NSArray *)paths {
    if (paths.count == 0) {
        return nil;
    }
    
    CGPoint *points = malloc(sizeof(CGPoint) * paths.count);
    for (int i = 0; i < paths.count; ++i) {
        points[i] = [[paths objectAtIndex:i] CGPointValue];
    }
    
    UIGraphicsBeginImageContext(CGSizeMake(self.size.width * self.scale, self.size.height * self.scale));
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(context);
    CGContextAddLines(context, points, paths.count);
    CGContextClosePath(context);
    CGRect boundsRect = CGContextGetPathBoundingBox(context);
    UIGraphicsEndImageContext();
    
    UIGraphicsBeginImageContext(boundsRect.size);
    context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, CGRectMake(0, 0, boundsRect.size.width, boundsRect.size.height));
    
    CGMutablePathRef  path = CGPathCreateMutable();
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-boundsRect.origin.x, -boundsRect.origin.y);
    CGPathAddLines(path, &transform, points, paths.count);
    
    CGContextBeginPath(context);
    CGContextAddPath(context, path);
    CGContextClip(context);
    
    [self drawInRect:CGRectMake(-boundsRect.origin.x, -boundsRect.origin.y, self.size.width * self.scale, self.size.height * self.scale)];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    CGPathRelease(path);
    UIGraphicsEndImageContext();
    CGContextRelease(context);

    return image;
}

 

展开阅读全文
打赏
1
0 收藏
分享
加载中
干货
2016/09/09 11:09
回复
举报
更多评论
打赏
1 评论
0 收藏
1
分享
返回顶部
顶部