2010-04-19 92 views
0

我正在制作一个应用程序,用户在其中选择图像,通过缩放(在uiscrollview中)并在视图中移动它来调整它的大小。完成后,我想保存图像,就像我们可以在uiscrollview中看到的一样。你有什么主意吗 ?如何“裁剪”一个uiimage?

谢谢。

回答

1

下面是代码......

这里frontGround是一个imageview的并且 testScrool是滚动视图

希望这会为你工作。

UIGraphicsBeginImageContext(testScrool.frame.size); 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
CGRect clippedRect = CGRectMake(0, 0, testScrool.frame.size.width, testScrool.frame.size.height); 
CGContextClipToRect(currentContext, clippedRect); 
CGRect drawRect = CGRectMake(testScrool.frame.origin.x * -1, 
          testScrool.frame.origin.y * -1,frontGround.frame.size.width,       frontGround.frame.size.height); 
CGContextDrawImage(currentContext, drawRect, frontGround.image.CGImage); 
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
frontGround.image = cropped;