【问题标题】:Fastest approach for rotating an image on the iPhone在 iPhone 上旋转图像的最快方法
【发布时间】:2009-06-22 09:23:41
【问题描述】:

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

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

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

注意:stackoverflow answer 中描述了一种方法,但我不确定这是否是最佳的。我当然会尝试一下,但我很想知道这是否被认为是“最佳实践”。

【问题讨论】:

    标签: iphone graphics uiimage


    【解决方案1】:

    这个将核心动画层应用到 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"];
    

    【讨论】:

    • 感谢分享这个核心动画变体。我也试试看。
    • 我同意核心动画的建议。如果您通过 Quartz 进行动画旋转,您将看到糟糕的性能,因为您需要进行所有重绘。您可能还想查看stackoverflow.com/questions/486609/…stackoverflow.com/questions/839296/…
    • rotatingImage 不应该是 UIImageView 吗?并用 someImage.png 设置它的图像,因为 UIImage 没有图层属性。
    【解决方案2】:

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

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

    就这么简单,对于像你所说的这样的小图像来说,这是高性能的——所有 UIKit 视图都是层支持的,所以它是硬件加速的。这个属性也是动画的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 2011-01-07
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多