![](/img/trans.png)
[英]How to set image of the UIImageView from another ViewController using StoryBoard?
[英]How to set image to UIImageView which is fetching asynchronously in another viewController?
我對IOS編程比較陌生。
我有一個NSObject
“地方”,如下所示。
@interface Places : NSObject
@property(strong) NSString *placeName;
@property(strong) UIImage *placeImage;
@end
我在UITableView中列出了Places對象的數組。 在tableView:cellForRowAtIndexPath:
正在從Web URL異步獲取圖像。 在tableView:didSelectRowAtIndexPath:
,我將相應的“ Places”對象傳遞給另一個ViewController(detailViewController)。 在那個detailViewController中,我使用以下代碼在UIImageView
顯示圖像。
self.imageView.Image = self.myPlaces.placeImage
我的問題是當我傳遞該對象時,無需將圖像提取到placeImage。 無論如何,在self.imageView
中連續完成圖像提取到placeImage
后,是否要更新self.imageView。
我的代碼如下。 在mainViewController的tableView:cellForRowAtIndexPath:
中調用它。
NSURL *url = [NSURL URLWithString:@"http://imageurl_goes_here"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
if(imageData != nil)
{
place.placeImage = [UIImage imageWithData: imageData];
cell.cellImageView.image = place.placeImage;
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.