UIImage的截取

本文参考 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;
}

 

转载于:https://my.oschina.net/u/1432769/blog/743447

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值