2011-05-17 48 views

回答

2

终于成功地旋转裁剪缩放的图像。

1)需要裁剪整个缩放或旋转的图像。

2)从裁剪的图像中裁剪矩形。您将获得最终缩放或旋转的裁剪图像。

张贴示例代码,它可能会对其他人有帮助。

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 


CGRect rect = CGRectMake(100,100 ,200, 200);//Rect we need from scaled or rotated image. 
CGImageRef cropped = CGImageCreateWithImageInRect(viewImage.CGImage, rect); 
UIImage *img = [UIImage imageWithCGImage:cropped]; 
CGImageRelease(cropped); 

谢谢。

+0

没有。它不起作用。 – Mercurial 2013-09-12 08:37:42

+0

它会很好地工作..请分享您的请求。 – Madhu 2013-09-12 14:35:33

+0

您的图像是旋转还是视图?在我的情况下,imageview被旋转和缩放,而不是图像本身。 – Mercurial 2013-09-24 12:11:16

0

试试这个(如果你需要裁剪的角落):

UIImageView *imageSubview = [[UIImageView alloc] initWithFrame:CGRectMake(50.0f, 5.0f,80.0f, 60.0f)]; 

[imageSubview setImage:actualImage]; 
[imageSubview setContentMode:UIViewContentModeScaleToFill]; 
[imageSubview.layer setCornerRadius:10.0]; 
[imageSubview.layer setMasksToBounds:YES]; 
[cell addSubview:imageSubview]; 

只是增加半径,你将使它圈,或走的这条路,你会得到你的实际解决方案上:)

+0

感谢您的更新。我需要图像的中间部分,或图像视图中的任何位置,如图像选择器图像编辑属性! – Madhu 2011-05-17 11:44:07

0
UIImage *myImage = [UIImage imageNamed:@"photo.png"]; 

CGRect cropRect = CGRectMake(0.0, 0.0, 320.0, 44.0)); 
CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect); 

UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect]; 
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 

CGImageRelease(croppedImage); 

其用于裁剪特定的高度和宽度

+0

感谢您的更新。 但已经与逻辑工作,工作与固定图像。一旦缩放或旋转的图像,它不工作,总是返回相同的图像。 CGRect rect = CGRectMake([tempView center] .x-125,[tempView center] .y-200,200,200); CGImageRef裁剪= CGImageCreateWithImageInRect(imageView.image.CGImage,矩形); UIImage * img = [UIImage imageWithCGImage:cropped]; // [UIImage imageWithCGImage:裁剪比例:imageView.image.scale orientation:imageView.image.imageOrientation]; CGImageRelease(裁剪); – Madhu 2011-05-17 11:41:28