iOS_UIImage_图片剪切
Posted iOS_满聪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS_UIImage_图片剪切相关的知识,希望对你有一定的参考价值。
- (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_图片剪切的主要内容,如果未能解决你的问题,请参考以下文章