gpt4 book ai didi

ios - 无法更改 UIImageView 图像

转载 作者:行者123 更新时间:2023-11-29 02:42:18 24 4
gpt4 key购买 nike

我有一个 IBOutletUIImageView,叫做 attachmentImage。默认情况下,它有一个通过 Storyboard添加的占位符图像。

如果图像已经存在,则使用 setImageWithURL 显示它。这有效并显示图像。从 viewDidLoad

调用
[self.attachmentImage setImageWithURL:[NSURL URLWithString:attachmentImageURL]];
self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill;
self.attachmentImage.clipsToBounds = YES;
[attachmentImage setNeedsDisplay];

如果图片不存在,则从库中选择并添加;

- (void)takeController:(FDTakeController *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info {
[attachmentImage setImage:nil];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill;
[self.attachmentImage setImage:photo];
self.attachmentImage.clipsToBounds = YES;
[attachmentImage setNeedsDisplay];
}

如果没有已添加的图像(占位符除外),则上传有效。但是,如果 ImageView 具有使用 setImageWithURL 设置的图像,则该图像不会被替换。

为什么会这样?帮助!

编辑 现在图像确实发生了变化,但会在一两秒后恢复原状。这是我对 setImageWithURL 函数所做的更改;

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
attachmentImageURL = [NSString stringWithFormat:@"%@%@", BaseURL, imgpath];
NSURL *url = [NSURL URLWithString:attachmentImageURL];
NSData *data = [NSData dataWithContentsOfURL:url];

if (![data isEqualToData:nil]) {
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
attachmentImage.image = image;
self.attachmentImage.contentMode = UIViewContentModeScaleAspectFill;
self.attachmentImage.clipsToBounds = YES;
[attachmentImage setNeedsDisplay];

[self applyImageViewConstraints];
});
}

});

这么近。

最佳答案

我会先尝试删除 setNeedsDisplay - 在大多数情况下 UIImageView 不需要

如果这不起作用,则您的代码的第一部分似乎由于某种原因在第二部分之前运行。我会进行一些记录调用以检查真正的执行顺序。

如果一切都失败了,你可以通过延迟调用来实现一个非常 hacky 的解决方案..

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),    
dispatch_get_main_queue(), ^{ .... }

此外,如果这解决了问题,它可以为您提供线索,提示您在那 2 秒内运行了一些意外代码。

关于ios - 无法更改 UIImageView 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25611262/

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