如何裁剪缩放,旋转图像视图的某些部分

时间:2011-05-17 10:08:51

标签: iphone uiimageview crop image-rotation image-scaling

我无法从图像视图裁剪图像,图像视图在裁剪之前进行缩放和旋转。使用现有的裁剪方法。但它仅适用于固定的图像视图。希望代码有用。

3 个答案:

答案 0 :(得分: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);

感谢。

答案 1 :(得分: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];

只需增加半径,你就可以使它成为圆形,或者在这条路径上行走,你将获得实际的解决方案。:)

答案 2 :(得分: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);

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

相关问题