我有一个IBOutlet
到UIImageView
调用attachmentImage
。默认情况下,它具有通过故事板添加的占位符图像。不能更改UIImageView图像
如果图像已经存在,则使用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];
});
}
});
太近了。
你能更好地解释你想要拥有什么,以及你有什么实际?你也可以张贴一些截图吗? – Serluca 2014-09-03 23:10:59
你正在从服务器获取图像? – 2014-09-08 07:37:35
我想你可能在设置新图片后调用了viewDidload。分开设置默认图像的代码(在viewDidload中),然后添加一个NSLog()以知道它何时被调用。 – 2014-09-09 14:46:03