首页 > 代码库 > iOS_UIImage_图片剪切
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_图片剪切
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。