在 UIImageView 中旋转图像

发布于 2024-12-01 07:07:17 字数 71 浏览 1 评论 0原文

是否可以仅旋转 UIImageView 中的图像?我正在寻找有关它的信息,但我只找到了如何旋转 UIImageVeiw 的信息。

Is it possible to rotate only image in UIImageView? I'm looking for information about it, but i only found information how rotate UIImageVeiw.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

转身泪倾城 2024-12-08 07:07:17

您可以使用以下代码旋转图像。注意,这使用了 CGImageRef,您可以通过 UIImage 获取它。

CGImageRef imageRef = [self CGImageRotatedByAngle:[image CGImage] angle:30];

一旦获得旋转的图像,您可以将 ImageView 的图像设置为新的旋转图像,如下所示:

UIImage* img = [UIImage imageWithCGImage: imageRef];
myImageView.image = img;

这是一个旋转 imageRef 的方法:

- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{

    CGFloat angleInRadians = angle * (M_PI / 180);
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGRect imgRect = CGRectMake(0, 0, width, height);
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                   rotatedRect.size.width,
                                                   rotatedRect.size.height,
                                                   8,
                                                   0,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedFirst);
    CGContextSetAllowsAntialiasing(bmContext, YES);
    CGContextSetShouldAntialias(bmContext, YES);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
    CGColorSpaceRelease(colorSpace);
    CGContextTranslateCTM(bmContext,
                          +(rotatedRect.size.width/2),
                          +(rotatedRect.size.height/2));
    CGContextRotateCTM(bmContext, angleInRadians);
    CGContextTranslateCTM(bmContext,
                          -(rotatedRect.size.width/2),
                          -(rotatedRect.size.height/2));
    CGContextDrawImage(bmContext, CGRectMake(0, 0,
                                             rotatedRect.size.width,
                                             rotatedRect.size.height),
                       imgRef);



    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
    CFRelease(bmContext);
    [(id)rotatedImage autorelease];

    return rotatedImage;
}

You can rotate the image with the following code. Note, this uses a CGImageRef which you can get from a UIImage via

CGImageRef imageRef = [self CGImageRotatedByAngle:[image CGImage] angle:30];

Once you get the rotated image, you can set the ImageView's image to your new rotated image like this:

UIImage* img = [UIImage imageWithCGImage: imageRef];
myImageView.image = img;

here is a method that will rotate the imageRef:

- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{

    CGFloat angleInRadians = angle * (M_PI / 180);
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGRect imgRect = CGRectMake(0, 0, width, height);
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                   rotatedRect.size.width,
                                                   rotatedRect.size.height,
                                                   8,
                                                   0,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedFirst);
    CGContextSetAllowsAntialiasing(bmContext, YES);
    CGContextSetShouldAntialias(bmContext, YES);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
    CGColorSpaceRelease(colorSpace);
    CGContextTranslateCTM(bmContext,
                          +(rotatedRect.size.width/2),
                          +(rotatedRect.size.height/2));
    CGContextRotateCTM(bmContext, angleInRadians);
    CGContextTranslateCTM(bmContext,
                          -(rotatedRect.size.width/2),
                          -(rotatedRect.size.height/2));
    CGContextDrawImage(bmContext, CGRectMake(0, 0,
                                             rotatedRect.size.width,
                                             rotatedRect.size.height),
                       imgRef);



    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
    CFRelease(bmContext);
    [(id)rotatedImage autorelease];

    return rotatedImage;
}
慢慢从新开始 2024-12-08 07:07:17

看一下旋转 ImageView 的图层。您将需要导入 QuartzCore 库以使其可访问。

myImageView.layer.transform = CGAffineTransformMakeRotation (1.5);

Take a look at rotating the ImageView's layer. You will need to import the QuartzCore library to make that accessible.

myImageView.layer.transform = CGAffineTransformMakeRotation (1.5);
深爱不及久伴 2024-12-08 07:07:17

请尝试这个,它对我有用:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:10];
myImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];

祝你好运!

Please try this, it works for me:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:10];
myImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];

Good luck!

放低过去 2024-12-08 07:07:17

以下代码可以在 Swift 中正常运行:

let img = UIImage(CGImage: self.imageView.image!.CGImage, scale: 1.0, orientation: UIImageOrientation.Right)
self.imageView.image = img
self.imageView.setNeedsLayout()

The following code works fine with Swift:

let img = UIImage(CGImage: self.imageView.image!.CGImage, scale: 1.0, orientation: UIImageOrientation.Right)
self.imageView.image = img
self.imageView.setNeedsLayout()
写下不归期 2024-12-08 07:07:17

如果您需要旋转/翻转图像以进行本地化,您可以使用以下命令:
imageView.image = imageView.image?.imageFlippedForRightToLeftLayoutDirection()

If you need to rotate/flip an image for localization purposes you can use the following:
imageView.image = imageView.image?.imageFlippedForRightToLeftLayoutDirection()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文