gpt4 book ai didi

ios - 由于图像加载问题,应用程序速度变慢

转载 作者:行者123 更新时间:2023-11-28 19:11:06 25 4
gpt4 key购买 nike

我在应用程序中加载内容时遇到问题,我看到数据是由应用程序获取的,但图像需要很长时间才能加载,是否有可能加载图像后记。代码如下:

NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row];
UIImageView *avatarimage = (UIImageView *)[cell viewWithTag:4];
NSString *photoStrn=[dict objectForKey:@"photo"];

dispatch_async(dispatch_get_global_queue(0,0), ^{
NSString *u=[NSString stringWithFormat:@"http://%@",photoStrn];
NSURL *imageURL=[NSURL URLWithString:u];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(), ^{
UIImage *dpImage = [UIImage imageWithData:imageData];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:@"profileImage.png"];
}
avatarimage.image = dpImage;

});

如果您需要更多详细信息,我会提供:)

最佳答案

您可以使用 GCD为此:

dispatch_async(dispatch_get_global_queue(0,0), ^{
for (NSDictionary *dic in discussionArray)
{
NSString *photoStr=[dic objectForKey:@"photo"];
NSString * photoString=[NSString stringWithFormat:@"http://%@",photoStr];
UIImage *dpImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:@"profileImage.png"];
}
}
});

关于ios - 由于图像加载问题,应用程序速度变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15407333/

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