在 iPhone 上旋转图像的最快方法

发布于 2024-07-25 07:50:51 字数 414 浏览 1 评论 0原文

我需要旋转 UIImage 以响应用户输入。 我希望它尽可能流畅,因此想知道哪种是执行转换和渲染的最快方法。 理想情况下,我想留在 UIKit 或 Quartz 框架内。 图像的属性如下:

  • 大小约为 250x300 像素
  • 不透明 - 需要使用 Alpha 通道渲染

实现此功能的最佳方法和实践是什么?

注意:stackoverflow 答案 但我不确定这是否是最佳的。 我当然会尝试一下,但我很想知道这是否被认为是“最佳实践”。

I need to rotate a UIImage in response to user input. I'd like this to be as slick as possible and therefore would like to know which is the quickest way to perform the transformation and render. Ideally I'd like to stay within the UIKit or Quartz frameworks. The properties of the image are as follows:

  • Size approximately 250x300 pixels
  • Not opaque - needs to render with an alpha channel

What are the best approaches and practices for implementing this functionality?

Note: There is an approach described in this stackoverflow answer but I am uncertain if this is optimal. I will of course give this a try but I'd be keen to know if this is considered to be the 'best practice'.

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

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

发布评论

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

评论(2

半窗疏影 2024-08-01 07:50:51

这个将核心动画层应用于 UIImage 的示例可能会提供一些启发:

UIImage* rotatingImage = [UIImage imageNamed:@"someImage.png"];

CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 10;

[rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

This example of applying a Core Animation layer to a UIImage might provide some inspiration:

UIImage* rotatingImage = [UIImage imageNamed:@"someImage.png"];

CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 10;

[rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
面犯桃花 2024-08-01 07:50:51

如果您在 UIImageView 中显示 UIImage,为什么不在视图上设置 Transform 属性呢?

myimageView.transform = CGAffineTransformMakeRotation(<angle in radians>);

就这么简单,对于像您所说的这样的小图像来说,这是高性能的 - 所有 UIKit 视图都是层支持的,因此它是硬件加速的。 该属性也是可动画的。

If you are displaying your UIImage in a UIImageView, why not just set the transform property on the view ?

myimageView.transform = CGAffineTransformMakeRotation(<angle in radians>);

It's as simple as that, and for a small image like your talking about this is highly performant - all UIKit views are layer backed, so it's hardware accelerated. This property is also animatable.

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