【问题标题】:Performance issue on IOS while redrawing image with gradient mask使用渐变蒙版重绘图像时 IOS 的性能问题
【发布时间】:2011-12-20 00:33:48
【问题描述】:

我正在使用陀螺仪来处理旋转。对于我旋转 iPad 的每一度,我都应该在屏幕上重新绘制图像以更改遮罩的高度。

但是重绘会使陀螺仪停止。

对于这种情况我该怎么办?

编辑添加代码

- (UIImage *)reflectedImage:(UIImageView *)fromImage withHeight:(NSUInteger)height
{
CGImageRef gradientMaskImage = CreateGradientImage(1, height);

CGImageRef masked = CGImageCreateWithMask([fromImage.image CGImage], gradientMaskImage);
CGImageRelease(gradientMaskImage);

UIImage *theImage = [UIImage imageWithCGImage:masked];

return theImage;
}

陀螺仪会给我一个值,我会计算图像的高度。之后我调用这个函数来重绘蒙版和图像。因此,如果我滚动设备,图像会向上或向下。

【问题讨论】:

  • 显示有问题的代码,以便我们可以使用。
  • 记住 Apple 示例应用程序的目的,这通常是最简单的展示。我发现CAReplicatorLayer 的性能非常好,如果可以的话尝试使用它。

标签: ios core-graphics gradient gyroscope


【解决方案1】:

您可以在后台线程中重绘图像。图像准备好后,在主线程上更新 UI。

static dispatch_queue_t background_queue;
- (void)updateImage {
  if (background_queue == nil){
        background_queue = dispatch_queue_create("com.myappname.myIdentifier", 0);
  }

  dispatch_async(background_queue, ^ {  // render the image in the background thread
     UIImage * theImage = [self reflectedImage:fromImage withHeight:height]; 
     dispatch_sync(dispatch_get_main_queue(), ^{
        imageView.image = theImage;    // Update the imageview in the main thread
     });
  });
}

编辑:代码修复

【讨论】:

  • 有实时解决方案吗?因为我猜这将是异步的。
  • 图像将以正确的顺序渲染和显示(即对于所有 t,frame(t) 将在 frame(t+1) 之前渲染。如果您需要更快的解决方案,那么您将拥有寻找优化图形渲染的方法。或者可能生成和缓存所有可能的图像(360?)
  • 其实我只想改变渐变层的高度。所以我不应该重绘所有的图像。但是我该怎么做呢?
  • 也许您可以尝试不同的混合模式?您必须重写 drawRect: 方法来进行自定义绘图。
猜你喜欢
  • 2022-06-29
  • 2014-06-23
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多