【发布时间】:2014-09-01 18:29:40
【问题描述】:
我有一个IBOutlet 到UIImageView,称为attachmentImage。默认情况下,它有一个通过情节提要添加的占位符图像。
如果图像已经存在,则使用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];
}
如果没有添加图像(占位符除外),则上传工作。但是,如果图像视图的图像设置为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];
});
}
});
如此接近。
【问题讨论】:
-
您能否更好地解释您想要拥有什么,以及您实际上拥有什么?你也可以发一些截图吗?
-
你正在从服务器获取图像??
-
我认为您的 viewDidload 在您设置新图像后可能已被调用。分离设置默认图像的代码(在您的 viewDidload 中),然后添加一个 NSLog() 以了解它何时被调用。
-
您说它会在 2 秒后恢复,您是否检查过您的 setImageWithURL 没有被多次调用?特别是 attachmentImage.image = image;
-
不是。解决了。添加答案。虽然不知道是什么导致了这个问题。 ://
标签: ios uiimageview uiimage