2
我想裁剪200 * 200的圖像,裁剪的部分是(x = 10,y = 10,w = 50,h = 50)。並繪製這部分新的500 * 500圖像,新矩形(x = 30,y = 30,w = 50,h = 50),該怎麼做?iOS如何將圖像的一部分繪製到特定的矩形?
我可以用下面的方法
- (UIImage*) getSubImageWithRect: (CGRect) rect {
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.size.width, self.size.height);
// clip to the bounds of the image context
// not strictly necessary as it will get clipped anyway?
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
// draw image
[self drawInRect:drawRect];
// grab image
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return subImage;
}
很抱歉,但你的代碼無法正常工作得到圖像的一部分。子圖像不正確。 –