我有一個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