gpt4 book ai didi

objective-c - 带有动态字符串的 UIImage 错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:11:38 25 4
gpt4 key购买 nike

我正在尝试将图像加载到单元格中,以下代码运行正常:

NSString *imageURL = @"http://localhost/images/image2.jpg";
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *rowImage = [[UIImage alloc] initWithData:imageData];

cell.image=rowImage;

但是当我将 imageURL 更改为动态时,图像不显示:

NSString *imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:@"image"];

您可能会说问题出在:

[[stories objectAtIndex:indexPath.row] objectForKey:@"image"];

但事实并非如此,我已经尝试记录它并且输出符合预期:

NSSLog(@"%@",[[stories objectAtIndex:indexPath.row] objectForKey:@"image"]);
//Outputs http://localhost/images/image2.jpg

有什么问题吗?

最佳答案

我猜字符串前后有空格。所以这样做,

NSString *imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:@"image"];
imageURL = [imageURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *rowImage = [[UIImage alloc] initWithData:imageData];

虽然这应该主要解决您的问题,但我建议您在 XML 解析期间在添加到字典之前修剪字符串。

您还应确保释放 imageDatarowImage。您可能已经这样做了,但它不在上面的代码中,所以我只是想指出它可能正在泄漏。

关于objective-c - 带有动态字符串的 UIImage 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6710688/

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