2012-06-19 28 views
-1

我正在開發一個iPad應用程序,我正在使用圖像上下文進行繪製。我在圖形上下文中添加了一個更多的圖像視圖來調整圖像大小。當我完成縮放圖像時,我需要裁剪兩幅圖像中公共區域的縮放圖像,並將裁剪後的圖像繪製到基本繪製圖像上。如何裁切圖像?

任何幫助將是可觀的。 在此先感謝。

回答

0

嘗試以下方法:

-(UIImage *)cropImage:(UIImage *)img fromRect:(CGRect)rect 
{ 
    CGFloat scale = [[UIScreen mainScreen] scale]; 
    if (scale > 1.0f) { 
     rect = CGRectMake(rect.origin.x * scale, 
          rect.origin.y * scale, 
          rect.size.width * scale, 
          rect.size.height * scale); 
    } 

    CGImageRef imageRef = CGImageCreateWithImageInRect(img.CGImage, rect); 
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:scale orientation:img.imageOrientation]; 
    CGImageRelease(imageRef); 
    return result; 
}