ios - 捕获 UIView 的屏幕截图 - 性能缓慢

标签 ios xcode uiimage thumbnails

我有一个绘图应用程序,我想创建 Canvas UIView(屏幕上和屏幕外)的快照,然后将其缩小。我所拥有的代码在 iPad 3 上永远是血淋淋的。模拟器没有延迟。 Canvas 为 2048x2048。

还有其他方法吗?或者我在代码中遗漏了什么?

谢谢!

-(UIImage *) createScreenShotThumbnailWithWidth:(CGFloat)width{
        // Size of our View
    CGSize size = editorContentView.bounds.size;


        //First Grab our Screen Shot at Full Resolution
    UIGraphicsBeginImageContext(size);
    [editorContentView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

        //Calculate the scal ratio of the image with the width supplied.
    CGFloat ratio = 0;
    if (size.width > size.height) {
        ratio = width / size.width;
    } else {
         ratio = width / size.height;
    }

        //Setup our rect to draw the Screen shot into 
    CGSize newSize = CGSizeMake(ratio * size.width, ratio * size.height);

        //Send back our screen shot
    return [self imageWithImage:screenShot scaledToSize:newSize];

}

最佳答案

您是否使用“Time Profiler”工具(“Product”菜单 -> “Profile”)来检查您在代码中的哪些地方花费了最多的时间? (当然,将它与您的设备一起使用,而不是模拟器,以进行逼真的分析)。我猜它不在你在问题中引用的图像捕获部分,而是在你的重新缩放方法中 imageWithImage:scaledToSize:方法。

与其在上下文中以其整个大小渲染图像,然后将图像重新缩放到最终大小,您应该通过对上下文应用一些仿射变换,以预期大小直接在上下文中渲染图层.

所以只需使用 CGContextConcatCTM(someScalingAffineTransform); UIGraphicsGetCurrentContext()在你的电话线之后 UIGraphicsBeginImageContext(size); , 应用缩放仿射变换,使图层以不同的比例/大小呈现。

通过这种方式,它将直接渲染为预期大小,速度会快得多,而不是以 100% 渲染,然后让您以耗时的方式重新缩放它

关于ios - 捕获 UIView 的屏幕截图 - 性能缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14785066/

相关文章:

iOS uisearchviewcontroller 用于 web 服务

c - 标准输入输出.h : No such file or directory

ios - react-native iOS新方案报错: Undefined symbol: _OBJC_CLASS_$_FlipperClient

ios - 从原始像素创建图像,但颜色无法根据 RGBA 十六进制值显示

ios - 更改内容 View 背景?

iphone - 辅助类/库使在 iOS 上使用 KVC/KVO 更实用/安全?

ios - UIButton 高度约束 swift 不起作用

objective-c - NSString 到 TimeInterval

swift - 收到错误 clang : error: linker command failed with exit code 1 (use -v to see invocation) swift

iphone - 如何将一张图像分割成多个部分?