gpt4 book ai didi

iphone - 使用图像快速加载 UIview 背景

转载 作者:行者123 更新时间:2023-11-29 13:14:17 25 4
gpt4 key购买 nike

我正在开发一个应用程序。我想将 uiview 背景设置为图像。该图像大小为 1.6 mb。我遵循以下代码。

UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];

但与小尺寸图像相比,它需要更多时间。所以请帮助我如何加载 3mb 大小的图像也像小尺寸图像一样。

最佳答案

您可以在后台队列上执行绘图操作,这样它就不会卡住 UI:

CGRect imgRect = self.view.bounds;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIGraphicsBeginImageContext(imgRect.size);
[[UIImage imageNamed:@"image.png"] drawInRect:imgRect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIColor* bgcolor = [UIColor colorWithPatternImage:image];
dispatch_async(dispatch_get_main_queue(), ^{
self.view.backgroundColor = bgcolor;
});
});

同时使用工具优化图像 ImageOptim .

关于iphone - 使用图像快速加载 UIview 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16231151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com