ios - 旋转 UIImage 或 UIImageView 自定义度数

标签 ios uiimageview uiimage image-rotation

我想旋转一个 UIImage 或 UIImageView,我尝试了很多代码,到处搜索,但我找不到有效的解决方案,我正在解释问题,我从网上下载了一张图片,然后我以这种方式调整它的大小:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();
return newImage;
}

调整大小效果很好,然后 我已将我的 UIImage 附加到我的 UIImageView,然后我以这种方式旋转了我的 UIImageView:

self.myImageView.transform = CGAffineTransformMakeRotation(degreesToRadian(-2));

但在这种情况下,边框是像素化的并且存在锯齿,所以我尝试先旋转 UIImage,然后将图像提供给我的 UIImageView,我发现了很多类似的代码,特别是我使用了这个类扩展名:

http://www.catamount.com/forums/viewtopic.php?f=21&t=967

这样一来,边界被抗锯齿化了,但图像质量下降了,变得更加模糊,而且不像第一次旋转那样锐利,有人有好的解决方案吗?

谢谢...

最佳答案

对于旋转图像..你可以使用这个IBAction ...每次点击按钮,图像将旋转90度...

-(IBAction)rotateImageClick:(id)sender{

UIImage *image2=[[UIImage alloc]init];
image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(90)]; //Angle by 90 degree
self.roateImageView.image = image2;
imgData= UIImageJPEGRepresentation(image2,0.9f);

对于旋转图像,你只需要为以下方法传递 UIimage 和旋转度数

- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees

//-------------------------------------------- ----------------------------

#pragma mark - imageRotatedByDegrees Method

- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{

    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];

    CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
    rotatedViewBox.transform = t;

    CGSize rotatedSize = rotatedViewBox.frame.size;
    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, (degrees * M_PI / 180));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

我想这个链接会有帮助......!通过单击 objective-c 中的按钮旋转原始图像

http://adrianmobileapplication.blogspot.com/2015/03/rotate-original-image-by-clicking.html

关于ios - 旋转 UIImage 或 UIImageView 自定义度数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10207374/

相关文章:

iphone - 如何为 UITableViewCell 的 ImageView 设置固定大小?

ios - 如何制作可点击的uiimage?

ios - 自定义 CollectionViewCell 的 ImageView 在应该配置时为 nil

swift - 快速从图库中选择照片不起作用

iphone - 使用 UIImage 和 caf 创建视频文件的问题

iphone - 如何将数据从一个 View 传回 IOS 中的另一个 View ?

iphone - 如何使用命令行将 ipa/app 文件安装到 iPhone 中?

iphone - Cocoa Touch 是否有与 Adob​​e Flex 的 HBox 或 VBox 等效的 UIView 或组件?

javascript - Css3 动画跳转到移动设备上的最后一个关键帧而不设置动​​画

ios - 为什么我的 UIGraphicsImageRenderer 没有绘图 (iOS 10)?