1

我想旋转 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

并且通过这种方式,边界被抗锯齿,而是损失了图像质量,它更加模糊,并且不像第一次旋转它那样锐利,有人有好的解决方案吗?

谢谢...

4

1 回答 1

0

用于旋转图像..您可以使用此 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

于 2015-03-02T11:19:26.730 回答