美文网首页
自定义区域裁剪UIImage

自定义区域裁剪UIImage

作者: A_SJP | 来源:发表于2019-03-22 16:33 被阅读0次

使用 CGImageRef 简单裁剪图片

需求:

长方形头像,只显示上半部分,且最终显示形状为圆形

效果如下:

黑线中的内容即裁剪后的效果

image.jpeg

解决思路:

把长方形图片,裁剪为正方形,并对UIImageView做圆角处理

实现代码:

// 原始图片
UIImage *image = [UIImage imageNamed:@"image.jpeg"];
// 图片处理对象
CGImageRef imageRef =image.CGImage;
// 裁剪区域
CGRect cutArea = CGRectMake(0,
                            0,
                            image.size.width,
                            image.size.width);
// 裁剪后的图片
CGImageRef cgImage = CGImageCreateWithImageInRect(imageRef,
                                                  cutArea);
UIImage *cutImage = [[UIImage alloc] initWithCGImage:cgImage];

相关文章

网友评论

      本文标题:自定义区域裁剪UIImage

      本文链接:https://www.haomeiwen.com/subject/fcsrvqtx.html