我在 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 似乎才能刷新自己。