iphone - 旋转和裁剪图像IOS

标签 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];

最佳答案

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

输入数据为inAngle(弧度角)和inImage(UIImage实例)。

这样做会创建一个图像上下文,对其应用转换,然后将原始图像绘制到该上下文中。生成的图像数据现在将存储在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();

关于iphone - 旋转和裁剪图像IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12061691/

相关文章:

android - 手机应用图片

ios - 将 IOS 中的应用程序功能限制为仅使用 Cell/WiFi 数据。

ios - 图像未从 iOS 中的文件路径加载

iphone - 如何让scrollview内容循环播放?

ios - 我应该在播放列表实体中使用什么删除规则?

iphone - 如何创建一行 UILabels 的 "container" View ?

iphone - Storyboard和 View 转换

iphone - 用渐变抚摸 CGPath?

ios - React Native 项目从 Xcode 运行,但不从命令行运行

iphone - 从 subview 使用 presentModalViewController