使用渐变遮罩重绘图像时IOS上的性能问题
我用陀螺仪来控制旋转。对于我旋转iPad的每一个角度,我都应该在屏幕上重新绘制图像,以改变遮罩的高度 但重新绘制会停止陀螺仪 我能为这种情况做些什么 编辑添加的代码使用渐变遮罩重绘图像时IOS上的性能问题,ios,core-graphics,gradient,gyroscope,Ios,Core Graphics,Gradient,Gyroscope,我用陀螺仪来控制旋转。对于我旋转iPad的每一个角度,我都应该在屏幕上重新绘制图像,以改变遮罩的高度 但重新绘制会停止陀螺仪 我能为这种情况做些什么 编辑添加的代码 - (UIImage *)reflectedImage:(UIImageView *)fromImage withHeight:(NSUInteger)height { CGImageRef gradientMaskImage = CreateGradientImage(1, height); CGImageRef masked
- (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;
}
陀螺仪会给我一个值,我会计算图像的高度。之后,我调用这个函数来重画遮罩和图像。因此,如果我滚动设备,图像将上盲或下盲。您可以在背景线程中重新绘制图像。当映像准备就绪时,更新主线程上的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
});
});
}
编辑:代码修复显示有问题的代码,这样我们就有了一些东西可以使用。记住苹果示例应用程序的用途,这通常是最简单的展示。我发现
CAReplicatorLayer
性能非常好,如果可能的话试着使用它。有实时解决方案吗?因为我想这将是异步的。图像将以正确的顺序呈现和显示(即,对于所有t帧,帧(t)将在帧(t+1)之前呈现。如果您需要更快的解决方案,那么您必须找到一些方法来优化图形呈现。或者可能生成并缓存所有可能的图像(360?)实际上我只想改变渐变层的高度。所以我不应该重画所有的图像。但是我该怎么做呢?也许你可以尝试不同的混合模式?你必须重写drawRect:方法来做你的自定义绘图。