문제

I am using the following code to save a frame of a movie to my desktop:

NSCIImageRep *imageRep = [NSCIImageRep imageRepWithCIImage:[CIImage imageWithCVImageBuffer:imageBuffer]];
NSImage *image = [[[NSImage alloc] initWithSize:[imageRep size]] autorelease];
[image addRepresentation:imageRep];
CVBufferRelease(imageBuffer);

NSArray *representations = [image representations];
NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:representations usingType:NSJPEGFileType properties:nil];
[bitmapData writeToFile:@"/Users/ricky/Desktop/MyImage.jpeg" atomically:YES];

At the second last line of code, I receive the following messages in the console, with no result being saved to the desktop:

<Error>: CGImageDestinationFinalize image destination does not have enough images
CGImageDestinationFinalize failed for output type 'public.jpeg'

The NSImage is still an allocated object for the entire method call, so I'm not sure why I am receiving complaints about insufficient amount of images.

I'd appreciate any help. Thanks in advance, Ricky.

도움이 되었습니까?

해결책

I think the source of the problem is that you're passing an array of NSCIImageRep objects to representationOfImageRepsInArray:usingType:properties:, which I believe expects an array of NSBitmapImageRep objects.

What you want to do is create an NSBitmapImageRep from your CIImage. Then you can use that to write to disk. That would be roughly:

CIImage *myImage = [CIImage imageWithCVImageBuffer:imageBuffer]; 
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCIImage:myImage];
NSData *jpegData [bitmapRep representationUsingType:NSJPEGFileType properties:nil];
[jpegData writeToFile:@"/Users/ricky/Desktop/MyImage.jpeg" atomically:YES];

Of course, you'd want to handle any error cases and probably pass a properties dictionary to fine-tune the JPEG creation.

다른 팁

I'm sorry i don't really know why your code doesn't work, but approaching it a different way (and i think more efficiently than your CVImageBuffer to CIImage to NSCIImageRep to NSImage to NSData, albeit at a slightly lower level):-

  1. CVImageBuffer to CGImage
  2. CGImage to jpg file

I don't have code ready made to do this but extracting the right stuff from those examples should be straight forward.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top