我想要旋转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
并且通过这种方式,边框是防褪色的,但是图像质量损失,它更模糊,并且不像旋转它那样尖锐,任何人都有一个很好的解决方案?
感谢...
答案 0 :(得分: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