效果图:
代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 50, 100, 100)]; imageView.image=[self changeimagetocilce:[UIImage imageNamed:@"1.png"]]; [self.view addSubview:imageView]; } //将图片处理成圆的 -(id)changeimagetocilce:(UIImage*)image { UIImage *finalImage = nil; UIGraphicsBeginImageContext(image.size); { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGAffineTransform trnsfrm = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(1.0, -1.0)); trnsfrm = CGAffineTransformConcat(trnsfrm, CGAffineTransformMakeTranslation(0.0, image.size.height)); CGContextConcatCTM(ctx, trnsfrm); CGContextBeginPath(ctx); CGContextAddEllipseInRect(ctx, CGRectMake(0.0, 0.0, image.size.width, image.size.height)); CGContextClip(ctx); CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, image.size.width, image.size.height), image.CGImage); finalImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return finalImage; }
时间: 2024-11-10 12:02:08