豆電雨 搬磚自味精:http://awhisper.github.io/2016/03/12/滾動圓角卡頓刨根問底/
使用Quartz2D直接繪制圖片
步驟:
a、創建目標大小(cropWidth,cropHeight)的畫布。
b、使用UIImage的drawInRect方法進行繪制的時候,指定rect為(-x,-y,width,height)。
c、從畫布中得到裁剪后的圖像。
- (UIImage*)cropImageWithRect:(CGRect)cropRect {
//豆電雨 CGRect drawRect = CGRectMake(-cropRect.origin.x , -cropRect.origin.y, self.size.width * self.scale, self.size.height * self.scale); UIGraphicsBeginImageContext(cropRect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClearRect(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height)); [self drawInRect:drawRect]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } @end