ios做不规则图片的镂空(中空)

教鞭 2020-04-11 09:09:37
问题:需要将效果图片中的人物图片周围画一个自定义的画框,比如说脚丫子,梅花图案等等,边框中间镂空后显示人物图片.
查看网络解决方案只有一个"CAShapeLayer和UIBezierPath",但是这些方案我做不到指定的不规则图案.请问大神如何解决?
边框图案:

效果图片:
...全文
476 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
教鞭 2020-04-11
  • 打赏
  • 举报
回复
解决方案:使用蒙板即可. // // ViewController.m // TestBKColor // // Created by frank.zhai on 2020/4/2. // Copyright © 2020 frank.zhai. All rights reserved. // #import "ViewController.h" #import "MyView.h" @interface ViewController (){ } @end @implementation ViewController UIImageView *imageV; - (void)viewDidLoad { [super viewDidLoad]; UIImage *imgSource = [UIImage imageNamed:@"image原图"]; UIImage *imgMask = [UIImage imageNamed:@"a_41_2"]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 300, 300)]; imgView.layer.borderWidth = 3; imgView.backgroundColor = [UIColor whiteColor]; imgView.image = [ViewController maskImage:imgSource withMask:imgMask]; [self.view addSubview:imgView]; } +(UIImage*)maskImage:(UIImage*)originImage toPath:(UIBezierPath*)path{ UIGraphicsBeginImageContextWithOptions(originImage.size, NO, 0); [path addClip]; [originImage drawAtPoint:CGPointZero]; UIImage* img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } + (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage { CGImageRef maskRef = maskImage.CGImage; CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), NULL, false); CGImageRef sourceImage = [image CGImage]; CGImageRef imageWithAlpha = sourceImage; //add alpha channel for images that don't have one (ie GIF, JPEG, etc...) //this however has a computational cost // if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) { // // imageWithAlpha = CopyImageAndAddAlphaChannel(sourceImage); // } CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask); CGImageRelease(mask); // release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel if (sourceImage != imageWithAlpha) { CGImageRelease(imageWithAlpha); } UIImage* retImage = [UIImage imageWithCGImage:masked]; CGImageRelease(masked); return retImage; } //下面的是resize图片的代码 // resize the original image and return a new UIImage object + (UIImage *) resizeImage:(UIImage *)image size:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } @end

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧