我在UIImageView中显示图片时遇到问题。
这就是我在ShareViewController中获取图像的方式:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
for (NSItemProvider* itemProvider in ((NSExtensionItem*)self.extensionContext.inputItems[0]).attachments )
{
if([itemProvider hasItemConformingToTypeIdentifier:@"public.image"])
{
++counter;
[itemProvider loadItemForTypeIdentifier:@"public.image" options:nil completionHandler:
^(id<NSSecureCoding> item, NSError *error)
{
UIImage *sharedImage = nil;
if([(NSObject*)item isKindOfClass:[NSURL class]])
{
sharedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:(NSURL*)item]];
}
if([(NSObject*)item isKindOfClass:[UIImage class]])
{
sharedImage = (UIImage*)item;
}
if ([(NSObject *)item isKindOfClass:[NSData class]])
{
sharedImage = [UIImage imageWithData:(NSData *)item];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"kNotificationDidLoadItem" object:sharedImage];
}];
}
}
}
这是我在DetailsViewController中接收通知的方式:
- (void)didLoadSharedImage:(NSNotification *)notification {
UIImage *sharedImage = [notification object];
[self.sharedImages addObject:sharedImage];
if (!self.theImageView.image) {
self.theImageView.image = sharedImage;
}
}
因此在此方法之后没有可见的图像。我调试了它,我看到那个图像在那里,但是当我按下新的视图控制器表单DetailsViewController然后弹回时,我看到它很奇怪。只有这样UIImageView似乎才能刷新自己。
答案 0 :(得分:3)
您是否尝试将didLoadSharedImage执行到UIThread中?喜欢
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.theImageView.image) {
self.theImageView.image = sharedImage;
}
})
祝你好运