码迷,mamicode.com
首页 > 移动开发 > 详细

iOS_UIImage_图片剪切

时间:2016-12-06 16:36:37      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:ntc   color   creat   使用   开启   turn   ubi   height   截取   

- (UIImage *)imagecutWithRect:(CGRect)rect {

    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    
    CGRect smallRect = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
    // 开启图形上下文
    UIGraphicsBeginImageContext(smallRect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextDrawImage(context, smallRect, subImageRef);
    
    UIImage * image = [UIImage imageWithCGImage:subImageRef];
    
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    
    CGImageRelease(subImageRef);
    
    return image;
}

使用

- (UIImageView *)imageView {
    if (_imageView == nil) {
        self.imageView = [[UIImageView alloc] init];
        self.imageView.backgroundColor = [UIColor redColor];
                
        UIImage * image = [UIImage imageNamed:@"1.jpg"];
        // 截取原图中间300*300
        CGFloat cutImageWH = 300;
        CGFloat cutImage_x = (image.size.width - 300) / 2;
        CGFloat cutImage_y = (image.size.height - 300) / 2;
        
        self.imageView.image = [image imagecutWithRect:CGRectMake(cutImage_x, cutImage_y, cutImageWH, cutImageWH)];
    } return _imageView;
}

 

iOS_UIImage_图片剪切

标签:ntc   color   creat   使用   开启   turn   ubi   height   截取   

原文地址:http://www.cnblogs.com/mancong/p/6138057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!