views:

20

answers:

0

I'm writing a scanning application that is getting images from a document scanner, turning them into NSImages and then displaying them in an IKImageView. About half of the time, however, when I try to call setImage:ImageProperties in IKImageView, it just hangs.

It only happens when I'm getting the image directly from the scanner, as I'm scanning. if I've gotten pages from the scanner, but not displayed them, but then after I step scanning, page up and down through pages, it works fine and never hangs (as far as I've seen).

The NSImage that gets scanned gets turned into a PDFPage and then inserted into a PDFDocument. Then, when I want to display a page, I get the image from the Page and display it. (Yes, I take an NSImage, turn it into a PDF, and then turn it back into an NSImage...)

Here is some code:

This is in my IKImageView subclass ( that also replaces isFlipped, and sets animates to NO)

-(void) setNSImage:(NSImage*) nsImage
{
    CGImageRef cgImage = nsImageToCGImage(nsImage);
    [self setImage:cgImage imageProperties:NULL];
}

CGImageRef nsImageToCGImage(NSImage* image)
{
  NSData * imgData = [image TIFFRepresentation];
  CGImageRef imgRef = 0;
  if(imgData)
  {
      CGImageSourceRef imageSource = 
                   CGImageSourceCreateWithData((CFDataRef)imgData,  NULL);

      imgRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
  }

  return imgRef;
}

To get the NSImage from the PDFDocument:

-(NSImage*) currentImage
{
    return [[NSImage alloc] initWithData: [[doc pageAtIndex: pageNum] dataRepresentation]];
}

Here's the call stack where I hang:

#0  0x94a3a066 in __semwait_signal
#1  0x94a39d22 in _pthread_cond_wait
#2  0x94a3b9b8 in pthread_cond_wait$UNIX2003
#3  0x95238477 in -[NSViewHierarchyLock _lockForWriting:handler:]
#4  0x9523824d in -[NSViewHierarchyLock lockForWritingWithExceptionHandler:]
#5  0x9527d871 in -[NSView setBoundsOrigin:]
#6  0x9527d68c in -[NSClipView setBoundsOrigin:]
#7  0x93721e03 in -[IKImageLayer autoResizeToRect:allowZoomIn:]
#8  0x9371f228 in -[IKImageLayer _updateLayer:]
#9  0x93723098 in -[IKImageLayer doSetImage:imageProperties:imageState:options:]
#10 0x9371f5c1 in -[IKImageLayer setImage:imageProperties:imageState:options:]
#11 0x93731d35 in -[IKComposer setImage:imageProperties:imageState:options:]
#12 0x9372ace1 in -[IKImageView(IKPrivate) setImage:imageProperties:imageState:options:]
#13 0x9372c421 in -[IKImageView setImage:imageProperties:]
#14 0x00005bb4 in -[FlippedIKImageView setNSImage:] at FlippedIKImageView.m:84

EDIT for more information:

I have page-up/down buttons that will scroll through a document, the IKImageView never hangs on those calls. If I get the scanner to produce a whole bunch of images by hitting the flatbed button a bunch of times, it never hangs. It only hangs if I'm getting the images from the document feeder. The images are all perfectly fine.

I thought it might be about timing, that the feeder images come much faster than flatbed images, but the up-down buttons are really fast too and that doesn't seem to break it.