gpt4 book ai didi

ios - 在 dispatch_async 中收到内存警告

转载 作者:可可西里 更新时间:2023-11-01 05:33:18 24 4
gpt4 key购买 nike

这是我在 cellForRowAtIndex 中编写的用于下载图像的代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

@autoreleasepool {
__block UIImage * img;
__block NSData *data;
if(![messageDocument.SmallImageURL isEqual:@""])
{
data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:messageDocument.SmallImageURL]];
img = [UIImage imageWithData:data];
}

dispatch_async(dispatch_get_main_queue(), ^{
imgUser.image = img;
img = nil;

imgUser.contentMode = UIViewContentModeScaleAspectFill;
CGSize size;
if(imgUser.image.size.width > imageFrame.size.width || imgUser.image.size.height > rectImage.size.height)
{
if(imgUser.image.size.width < imageFrame.size.width)
{
rectImage.size.width = imgUser.image.size.width;
}

if(imgUser.image.size.height < rectImage.size.height)
{
rectImage.size.height = imgUser.image.size.height;
}

size = CGSizeAspectFit(imgUser.image.size, rectImage.size);
imgUser.frame = CGRectMake(imgUser.frame.origin.x, rectImage.origin.y, size.width, size.height);

height = imgUser.frame.size.height;
}
else
{
imgUser.frame = CGRectMake(imageFrame.origin.x, imageFrame.origin.y, imgUser.image.size.width, imgUser.image.size.height);
height = imgUser.image.size.height;

}

CGPoint contentOffset = tableMessageDetail.contentOffset;
[tableMessageDetail beginUpdates];
[tableMessageDetail endUpdates];
[tableMessageDetail setContentOffset:contentOffset];
});

messageDocument.Pic = data;
data = nil;
if(messageDocument.Pic != nil)
{
Attachment *attachment = [Attachment new];
attachment.DocId = messageDocument.DocId;
attachment.DocURL = messageDocument.DocURL;
attachment.ImageId = messageDocument.ImageId;
attachment.MessageId = messageDocument.MessageId;
attachment.SmallImageURL = messageDocument.SmallImageURL;
attachment.OriginalFileName = messageDocument.OriginalFileName;
if([messageDocument.DocURL isEqual:@""])
{
NSArray *attachmentArray = [messageDocument.SmallImageURL componentsSeparatedByString:@"/"];
NSString *attachmentName = [attachmentArray objectAtIndex:attachmentArray.count - 1];
attachment.AttachmentName = attachmentName;
}
else
{
NSArray *attachmentArray = [messageDocument.DocURL componentsSeparatedByString:@"/"];
NSString *attachmentName = [attachmentArray objectAtIndex:attachmentArray.count - 1];
attachment.AttachmentName = attachmentName;
}

attachment.Pic = messageDocument.Pic;
[[CommonModel shared]CreateAttachment:attachment];
[[CommonModel shared]UpdateMessageDocumentPic:messageDocument];

attachment = nil;
}
}
});

但是如果有超过 6 张图片,我会在控制台上收到内存异常并显示以下消息:

Message from debugger: Terminated due to memory issue

最佳答案

你的下载逻辑没有问题。问题是您试图将图像保存在阵列中,这最终会增加您的应用程序内存。由于您持有的图像是压缩的 png,它们似乎没有太大的尺寸,但它会大大增加您的应用程序的内存并可能使您的应用程序崩溃。

要解决您的问题,您需要在下载每个文件后立即将其保存到文档目录,然后保存图像的名称(如果需要在某处显示,则保存缩略图)而不是整个完整分辨率图片。

更新

您可以使用 Xcode 中的 instruments 工具分析应用程序的内存分配。它可以为您提供消耗最多内存的对象的提示。

关于ios - 在 dispatch_async 中收到内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991852/

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