gpt4 book ai didi

ios - 合并两个 UIImage 时收到内存警告

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

我尝试使用以下代码组合两个 UIImage:

- (void)combineImage:(UIImage *)image WithFrame:(CGRect)frame Completion:(ImageProcessorCompletionBlock)block {
__weak typeof(self) wSelf = self;

dispatch_async(_queue, ^{
if (wSelf) {
typeof(wSelf) sSelf = wSelf;

UIGraphicsBeginImageContextWithOptions(sSelf.originalImage.size, NO, 0.0);

[sSelf.originalImage drawInRect:CGRectMake(0, 0, sSelf.originalImage.size.width, sSelf.originalImage.size.height)];
[image drawInRect:frame];

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(result);
}
});
}
});
}

这行得通,但是当我检查内存使用情况时,它吓坏了我。每次我运行该方法时,内存都会上升并且永远不会释放。有时我会收到内存警告。任何人都可以告诉我为什么并给我一个解决问题的方法吗?非常感谢!

最佳答案

终于找到问题所在了。UIGraphicsBeginImageContextWithOptions(sSelf.originalImage.size, NO, 0.0);

第一个参数是图像的大小,最后一个是比例因子。一开始我已经将图像大小设置为与原始图像大小相同。但我也将比例设置为0.0,这意味着它设置为设备主屏幕的比例因子。所以 result 图像被放大了。

如果我多次运行代码,result 的大小会越来越大,最后它会耗尽内存,我会收到警告。

关于ios - 合并两个 UIImage 时收到内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43764508/

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