旋转 UIImageView 数据 -
Posted
技术标签:
【中文标题】旋转 UIImageView 数据 -【英文标题】:Rotating UIImageView data - 【发布时间】:2014-04-11 06:13:09 【问题描述】:您好,在我的 ios 应用程序中,我想将 UIImage
数据旋转 90 度。
我使用下面的代码进行 180 度旋转 - 效果很好。
- (UIImage*)upsideDownBunny:(UIImage *)img
CGSize imgSize = [img size];
UIGraphicsBeginImageContext(imgSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -imgSize.width, -imgSize.height);
[img drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
但是当我尝试旋转 90 度时它不起作用 - 请纠正我
- (UIImage *)upsideDownBunny:(UIImage *)img
CGSize imgSize = [img size];
UIGraphicsBeginImageContext(imgSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, M_PI_2);
CGContextTranslateCTM(context, -imgSize.width, -imgSize.height);
[img drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
【问题讨论】:
【参考方案1】:试试这个...
UIImage * LandscapeImage = [UIImage imageNamed: imgname];
UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
scale: 1.0
orientation: UIImageOrientationLeft];
希望对你有帮助!
【讨论】:
【参考方案2】:试试这个
- (UIImage *)upsideDownBunny:(UIImage *)image
// Rotate in degrees
CGFloat degrees = M_PI_2;
// Calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
CGAffineTransform transform = CGAffineTransformMakeRotation(degrees);
rotatedViewBox.transform = transform;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create image context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image.
CGContextTranslateCTM(context, rotatedSize.width * 0.5f, rotatedSize.height * 0.5f);
// Rotate the image context
CGContextRotateCTM(context, degrees);
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(-image.size.width * 0.5f, -image.size.height * 0.5f, image.size.width, image.size.height), [image CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
问题是您的上下文围绕左上角 (0,0) 旋转,旋转 90 度后它将超出范围。所以你需要把上下文的原点移到中间。
【讨论】:
这是可行的,但图像的高度会缩小。你能告诉我这个问题吗? @SplatterStrikes 新的会发生什么?只需点击已编辑...你会得到旧的。以上是关于旋转 UIImageView 数据 -的主要内容,如果未能解决你的问题,请参考以下文章