你如何在 iOS 中实现这样的像素化过渡(动画 gif)?
可以用Core Animation实现吗?
Core Image Filter Reference 中记录了 CIPixellate 和 CIHexagonalPixellate 过滤器,但这些目前仅适用于 OSX ......不幸的是,不适用于 iOS。
C4 - Travis 的评论是正确的,您最好的选择可能是自己渲染动画。您需要将QA1703中的代码用于屏幕捕获,但UIGraphicsBeginImageContextWithOptions
在调用UIGraphicsGetCurrentContext
. 所以,只要在我写这篇文章的时候打字,你的调整就会产生类似的结果:
- (UIImage*)screenshotWithScale:(CGFloat)scale
{
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
/* YOU'VE ADDED: */
imageSize.width *= scale;
imageSize.height *= scale;
if (NULL != UIGraphicsBeginImageContextWithOptions)
/* ... then stuff as per the original, down to ... */
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextScaleCTM(context, scale, scale);
// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
/* etc, etc, down to... */
// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
UIGraphicsEndImageContext();
return image;
}
使用 scale = 1.0,您将得到UIImage
与屏幕完全相等的 a。使用 scale = 0.5,您将获得一个具有一半像素的横向和向下像素,使用 scale = 0.25 您将获得四分之一的像素横向和向下,等等。
然后,您可以将其UIImage
放入UIImageView
,并将其图层的放大过滤器设置为kCAFilterNearest
。显示该图像视图应该为您提供一个故意像素化的原始版本。然后,您可以懒惰并继续对屏幕上已有的内容执行一半大小的渲染(因此第一次是实时视图,随后是图像视图),或者调整代码不从主窗口渲染,而是从指定的窗口渲染查看并根据需要从原始视图层次结构重新渲染(如果您想做一些事情而不是继续将比例除以整数,这将起作用)。
我不知道在过渡中使用它的好方法,但是如果您可以捕获内容的静止图像(作为 UIImage),您应该能够执行时间动画像素化过滤器来产生上述效果.
我在这个答案中展示了我的 GPUImagePixellateFilter 的示例,如果您调整fractionalWidthOfAPixel
计时器上的属性,它可能会产生这样的效果。
对于静止图像,您需要将 UIImage 作为 GPUImagePicture 拉入并通过像素化过滤器将其链接到 GPUImageView。对于像素宽度的每次更新,您都需要调用-processImage
图像源来更新屏幕上的图像。在初始设置和图像上传之后,该动画应该在每台支持 OpenGL ES 2.0 的 iOS 设备上以 60 FPS 的速度运行。