旋转和裁剪图像IOS

时间:2012-08-21 19:27:01

标签: iphone objective-c ios xcode

我正在UIImageView上执行轮播,然后我尝试裁剪其中的一部分并将其另存为UIImageUIImageView旋转,但它总是裁剪照片的相同部分。因此裁剪不考虑图像旋转。我究竟做错了什么?

//rotate image 
CGRect new = CGRectMake(0, 0, 100, 50);
CGAffineTransform rotation = CGAffineTransformMakeRotation(5);
[photo setTransform:rotation];

// crop image 
CGImageRef imageRef = CGImageCreateWithImageInRect([photo.image CGImage], new);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

// display crop bounds 
UIView* faceBounds = [[UIView alloc] initWithFrame:new];
faceBounds.layer.borderWidth = 2;
faceBounds.layer.borderColor = [[UIColor redColor] CGColor];

2 个答案:

答案 0 :(得分:1)

使用以下代码段旋转图像数据。

输入数据为inAngle(以弧度表示的角度)和inImageUIImage实例)。

这样做,它创建一个图像上下文,将变换应用于它并将原始图像绘制到该上下文中。生成的图像数据现在将存储在resultImage

前三行处理边界结果图像帧的计算。

UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, inImage.size.width, inImage.size.height)];
rotatedViewBox.transform = CGAffineTransformMakeRotation(inAngle);
CGSize rotatedSize = rotatedViewBox.frame.size;

UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0f, rotatedSize.height / 2.0f);
CGContextRotateCTM(bitmap, inAngle);
CGContextScaleCTM(bitmap, 1.0f, -1.0f);
CGContextDrawImage(bitmap, CGRectMake(-inImage.size.width / 2.0f,
                                      -inImage.size.height / 2.0f,
                                      inImage.size.width,
                                      inImage.size.height),
                                      inImage.CGImage);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 1 :(得分:-1)

现在为时已晚,但现在有人可以尝试this