iOS 利用Context裁剪图片
下面的代码可以裁剪出圆形的图片,
1,先把不规则图片转成正方形图片
UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.width)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
2 ,把正方形图像绘制剪切为圆形
UIGraphicsBeginImageContext(image.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); CGContextAddEllipseInRect(context, rect); CGContextClip(context); //进行剪切,所有context绘制只保留能够被当前path进行fill的区域 [image drawInRect:rect]; //绘制原图 UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();