ios – 捕获UIView的屏幕截图 – 性能缓慢
发布时间:2020-12-14 17:29:28 所属栏目:百科 来源:网络整理
导读:我有一个各种绘图应用程序,我想创建一个画布UIView的快照(打开和关闭屏幕),然后缩小它.我做的那些代码在iPad上永远都是血腥的3.模拟器没有延迟.画布是2048×2048. 还有另一种方法我应该这样做吗?或者我在代码中遗漏了什么? 谢谢! -(UIImage *) createScre
我有一个各种绘图应用程序,我想创建一个画布UIView的快照(打开和关闭屏幕),然后缩小它.我做的那些代码在iPad上永远都是血腥的3.模拟器没有延迟.画布是2048×2048.
还有另一种方法我应该这样做吗?或者我在代码中遗漏了什么? 谢谢! -(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”工具(“产品”菜单 – >“配置文件”)来检查代码中您花费大部分时间的位置? (当然,使用它与您的设备,而不是模拟器,以实现真实的分析).我猜它不是你在问题中引用的图像捕获部分,而是在你的重新缩放方法imageWithImage:scaledToSize:method中.
不是在上下文中以整个大小渲染图像,而是将图像重新缩放到最终大小,您应该通过对上下文应用一些仿射变换,直接以预期大小渲染上下文中的图层. 所以只需使用CGContextConcatCTM(someScalingAffineTransform);在你的行UIGraphicsBeginImageContext(size);之后的UIGraphicsGetCurrentContext()上,应用缩放仿射变换,这将使图层以不同的比例/大小呈现. 这样它将直接渲染为预期的大小,这将更快,而不是以100%渲染,然后让你以一种耗时的方式重新缩放它 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |