objective-c - UIImageView 不显示从通知中获取的 UIImage

标签 objective-c uiimageview uiimage nsnotificationcenter

我正在开发一个用于显示 PDF 文件的页面 View Controller ,我想在另一个线程中从 PDF 页面开始渲染 UIImage,并通过通知将渲染图像传递到主线程。

我首先创建了一种不同的方法,该方法在 UIImage 中呈现我的页面,并在图像准备就绪时发布通知,但至少现在我仍在主线程上执行此操作。

- (void)generatePage:(int)pageNumber withSize:(CGSize)size {
    UIImage *resultingImage;

    CGFloat sizeRatio = size.width/size.height;

    CGPDFDocumentRef pdfRef = [self PDFDocumentRef];
    CGPDFPageRef myPageRef = CGPDFDocumentGetPage(pdfRef, pageNumber);

    CGRect pageRect = CGPDFPageGetBoxRect(myPageRef, kCGPDFMediaBox);
    CGFloat pdfHWRatio = pageRect.size.width/pageRect.size.height;

    CGFloat pdfScale;

    if (sizeRatio < pdfHWRatio) {
        pdfScale = size.width/pageRect.size.width;
    }
    else {
        pdfScale = size.height/pageRect.size.height;
    }
    pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
    pageRect.origin = CGPointZero;


    UIGraphicsBeginImageContext(pageRect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context,pageRect);

    CGContextSaveGState(context);

    CGContextTranslateCTM(context, 0.0, pageRect.size.height);
    CGContextScaleCTM(context, pdfScale, -pdfScale);

    CGContextDrawPDFPage(context, myPageRef);
    CGContextRestoreGState(context);

    resultingImage = UIGraphicsGetImageFromCurrentImageContext();

    if (resultingImage) {
        [pagesCache setObject:resultingImage
                   forKey:[self generateCacheNameWithSize:size
                                            andPageNumber:pageNumber]];
        NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                                                  resultingImage,
                                                                  [NSNumber numberWithInt:pageNumber],
                                                                  nil]
                                                         forKeys:[NSArray arrayWithObjects:
                                                                  CatalogRenderedPageImageKey,
                                                                  CatalogRenderedPageNumberKey,
                                                                  nil]];

        NSNotification *note = [NSNotification notificationWithName:CatalogRenderPageNotification
                                                         object:self
                                                       userInfo:userInfo];
        [[NSNotificationCenter defaultCenter] postNotification:note];
    }


    UIGraphicsEndImageContext();

    CGPDFDocumentRelease(pdfRef);
}

我已成功收到通知并正确获取了 UIImage(相同的对象 ID),但 UIImageView 为空。

- (void) imageLoaded:(NSNotification *)note {
    NSDictionary *userInfo = note.userInfo;
    NSNumber *pageNumber = [userInfo objectForKey:CatalogRenderedPageNumberKey];

    if ([self.pageNumber isEqualToNumber:pageNumber]) {
        UIImage *image = [userInfo objectForKey:CatalogRenderedPageImageKey];
        self.image = image;
        self.imageView.image = _image;
        [[NSNotificationCenter defaultCenter] removeObserver:self name:CatalogRenderPageNotification object:self.catalog];
    }
}

我错过了什么?我已经检查了 UIImageView 中的存储属性,它正在填充 UIImage。

您可能已经注意到我将渲染的图像保存在缓存中,事实是,当我从缓存中获取图像时,我能够在 UIImageView 中看到它们,所以我排除了 UIImage 被渲染的可能性生成不好。


更新: 我开始在不同的线程中调用 generatePage:

dispatch_async(dispatch_queue_create("renderingQueue", DISPATCH_QUEUE_CONCURRENT), ^{
            [self generatePage:pageNumber withSize:size];
        });

并按照@phyx23 的建议在主线程中发布通知。它现在可以工作了,但我仍然不明白为什么它以前没有工作。

我将其开放,以便任何人都可以留下他们对为什么会发生这种情况的想法。

最佳答案

我的猜测是,问题的原因是当您将创建的图像分配给 ImageView 时,您仍然处于图像上下文中。您应该先使用 UIGraphicsEndImageContext 结束图像上下文,然后再发布通知。 此外,当您开始在后台执行它时,请确保您在主线程上发布通知。

resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdfRef);

if (resultingImage) {
  dispatch_async(dispatch_get_main_queue(), ^{
    [pagesCache setObject:resultingImage
               forKey:[self generateCacheNameWithSize:size
                                        andPageNumber:pageNumber]];
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                                              resultingImage,
                                                              [NSNumber numberWithInt:pageNumber],
                                                              nil]
                                                     forKeys:[NSArray arrayWithObjects:
                                                              CatalogRenderedPageImageKey,
                                                              CatalogRenderedPageNumberKey,
                                                              nil]];

    NSNotification *note = [NSNotification notificationWithName:CatalogRenderPageNotification
                                                     object:self
                                                   userInfo:userInfo];
    [[NSNotificationCenter defaultCenter] postNotification:note];
  });
}

关于objective-c - UIImageView 不显示从通知中获取的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129792/

相关文章:

ios - 如何在UIImageView中设置透明背景色

iOS - 将 UIImage 保存为灰度 JPEG

ios - 从 NSSet 获取随机对象不起作用

ios - 如何准确地在图像图案上画线

objective-c - OCMock valueForKey 因 NSPredicate 失败

ios - MKMapView 中的所有 Annotation 对用户不可见

iphone - 切换 UITableViewCell 中图像的显示

ios - 如何从 iOS 中的 ImageView 获取标签?

ios - UIImagePickerController Camera Overlay 匹配默认裁剪

IOS:在 View 中绘制图像