Is it possible to print IKImageBrowserView In Cocoa programmatically? - cocoa

I want to take print of IKImageBrowserView with (content) images. I tried the following code
if (code == NSOKButton) {
NSPrintInfo *printInfo;
NSPrintInfo *sharedInfo;
NSPrintOperation *printOp;
NSMutableDictionary *printInfoDict;
NSMutableDictionary *sharedDict;
sharedInfo = [NSPrintInfo sharedPrintInfo];
sharedDict = [sharedInfo dictionary];
printInfoDict = [NSMutableDictionary dictionaryWithDictionary: sharedDict];
[printInfoDict setObject:NSPrintSaveJob
forKey:NSPrintJobDisposition];
[printInfoDict setObject:[sheet filename] forKey:NSPrintSavePath];
printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
[printInfo setHorizontalPagination: NSAutoPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];
printOp = [NSPrintOperation printOperationWithView:imageBrowser
printInfo:printInfo];
[printOp setShowsProgressPanel:NO];
[printOp runOperation];
}
because IKImageBrowserView is Inherits from NSView but print preview is showing null image. Please help me to over come this problem. Thanks in advance.....

/*
1) allocate a c buffer at the size of the visible rect of the image
browser
*/
NSRect vRect = [imageBrowser visibleRect];
NSSize size = vRect.size;
NSLog(#"Size W = %f and H = %f", size.width, size.height);
void *buffer = malloc(size.width * size.height * 4);
//2) read the pixels using openGL
[imageBrowser lockFocus];
glReadPixels(0,
0,
size.width,
size.height,
GL_RGBA,
GL_UNSIGNED_BYTE,
buffer);
[imageBrowser unlockFocus];
//3) create a bitmap with those pixels
unsigned char *planes[2];
planes[0] = (unsigned char *) (buffer);
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:planes pixelsWide:size.width
pixelsHigh:size.height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES
isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bitmapFormat:0
bytesPerRow:size.width*4 bitsPerPixel:32];
/*
4) create a temporary image with this bitmap and set it flipped
(because openGL and the AppKit don't have the same pixels coordinate
system)
*/
NSImage *img = [[NSImage alloc] initWithSize:size];
[img addRepresentation:imageRep];
[img setFlipped:YES];
[imageRep release];
/*
5) draw this temporary image into another image so that we get an
image without any reference to our "buffer" buffer so that we can
release it after that
*/
NSImage *finalImage = [[NSImage alloc] initWithSize:size];
[finalImage lockFocus];
[img drawAtPoint:NSZeroPoint
fromRect:NSMakeRect(0,0,size.width,size.height)
operation:NSCompositeCopy fraction:1.0];
[finalImage unlockFocus];
//[NSString stringWithFormat:#"/tmp/%#.tiff", marker]
NSData *imageData = [finalImage TIFFRepresentation];
NSString *writeToFileName = [NSString stringWithFormat:#"/Users/Desktop/%#.png", [NSDate date]];
[imageData writeToFile:writeToFileName atomically:NO];
//6) release intermediate objects
[img release];
free(buffer);
After this I send imageData for print, which works great for me.

Related

Looking for simple example to write text into a NSImage using Cocoa

NSImage *myNewIconImage=[[NSImage imageNamed:#"FanImage"] copy];
[myNewIconImage lockFocus];
[#"15" drawAtPoint:NSZeroPoint withAttributes:nil];
[myNewIconImage unlockFocus];
[myNewIconImage setTemplate:YES];
[[NSApplication sharedApplication]] setApplicationIconImage:myNewIconImage];
I am looking for a way to simply write a String onto this image.... and coming up very short. This does not worker me.
The following code will place a mutable attributed string on an NSImage:
NSImageView *imageView = [[NSImageView alloc] initWithFrame:NSMakeRect( 0, 0, _wndW, _wndH )];
[[window contentView] addSubview:imageView];
NSImage *image = [NSImage imageNamed:#"myImage.jpg"];
[image lockFocus];
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
[attr setObject:[NSFont fontWithName:#"Lucida Grande" size:36] forKey:NSFontAttributeName];
[attr setObject: [NSNumber numberWithFloat: 10.0] forKey: NSStrokeWidthAttributeName];
[attr setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
NSString *myStr = #"Welcome to Cocoa";
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithString: myStr attributes:attr];
[s drawAtPoint:NSMakePoint(130,130)];
[image unlockFocus];
[imageView setImage:image];

How to shift image horizontally to create loop effect?

How to shift NSImage horizontally that shifted pixels appear at the other side so it looks like a loop?
Currently I am using drawInRect. Is there any CIFilter or smarter way to do this?
- (CIImage *)image:(NSImage *)image shiftedBy:(CGFloat)shiftAmount
{
NSUInteger width = image.size.width;
NSUInteger height = image.size.height;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:width
pixelsHigh:height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:0
bitsPerPixel:0];
[rep setSize:NSMakeSize(width, height)];
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
[NSGraphicsContext setCurrentContext:context];
CGRect rect0 = CGRectMake(0, 0, width, height);
CGRect leftSourceRect, rightSourceRect;
CGRectDivide(rect0, &leftSourceRect, &rightSourceRect, shiftAmount, CGRectMinXEdge);
CGRect rightDestinationRect = CGRectOffset(leftSourceRect, width - rightSourceRect.origin.x, 0);
CGRect leftDestinationRect = rightSourceRect;
leftDestinationRect.origin.x = 0;
[image drawInRect:leftDestinationRect fromRect:rightSourceRect operation:NSCompositingOperationSourceOver fraction:1.0];
[image drawInRect:rightDestinationRect fromRect:leftSourceRect operation:NSCompositingOperationSourceOver fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
return [[CIImage alloc] initWithBitmapImageRep:rep];
}
I tried it with CIFilter however the performance hit is 3-4x slower. However, the code is more readable.
- (CIImage *)image:(NSImage *)image shiftXBy:(CGFloat)shiftX YBy:(CGFloat)shiftY
{
//avoid calling TIFFRepresentation here cause the performance hit is even bigger
CIImage *ciImage = [[CIImage alloc] initWithData:[image TIFFRepresentation]];
CGAffineTransform xform = CGAffineTransformIdentity;
NSValue *xformObj = [NSValue valueWithBytes:&xform objCType:#encode(CGAffineTransform)];
ciImage = [ciImage imageByApplyingFilter:#"CIAffineTile"
withInputParameters:#{kCIInputTransformKey : xformObj} ];
ciImage = [ciImage imageByCroppingToRect:CGRectMake(shiftX, shiftY, image.size.width, image.size.height)];
return ciImage;
}
For optimum performance you need to use CALayer. Here’s the basic concept:
Your NSView subclass should have wantsLayer and layerUsesCoreImageFilters set to true.
Assign your image to content property of NSView.layer (or add a new sublayer).
Create CIAffineTile filter and add it to the layer.
Now you can change values of the filter without reloading or redrawing the image. This all would be hardware accelerated.

Merging 2 images in Cocoa on Retina Macs

I'm trying to merge two images to a background image. This works fine on non-Retina Macs.
The resulting image has the size of my original background image. But on a MacBook Pro Retina the resulting image is doubled. I read that one should use imageWithSize for solving that issue cause since 10.8 / Retina support images aren't measured in pixels anymore; as we know it's points.
I've no clue how I should change my code to solve this.
What i have so far:
NSSize size = background.size ;
GLsizei width = (GLsizei)size.width;
GLsizei height = (GLsizei)size.height;
NSBitmapImageRep* imgRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:width*3 bitsPerPixel:0];
#ifdef __MAC_10_8
NSImage* backgroundWrapper = [NSImage imageWithSize:size flipped:YES drawingHandler:^(NSRect dstRect){ return [imgRep drawInRect:dstRect]; }];
NSPoint backgroundPoint;
backgroundPoint.x = 0;
backgroundPoint.y = 0;
[backgroundWrapper lockFocusFlipped:YES];
[background drawAtPoint:backgroundPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[firstImage firstImage fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[backgroundWrapper unlockFocus];
NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:[backgroundWrapper TIFFRepresentation]];
[backgroundWrapper addRepresentation:bmpImageRep];
#else
[background lockFocusFlipped:YES];
[firstImage drawAtPoint:firstImagePoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[background unlockFocus];
NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:[background TIFFRepresentation]];
[background addRepresentation:bmpImageRep];
#endif
NSData *data = [bmpImageRep representationUsingType: NSPNGFileType properties: nil];
How can I get this to work on a Retina display?
edit
I also tried this:
- (NSImage )drawImage:(NSImage)background with:(NSImage*)firstImage {
return [NSImage imageWithSize:NSMakeSize(3200, 2000) flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
NSPoint backgroundPoint; backgroundPoint.x = 0;
backgroundPoint.y = 0;
[background drawAtPoint:backgroundPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[firstImage drawAtPoint:backgroundPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
return true;
}];
}
But the resulting image is 6400x4000px

Trying to resize an NSImage which turns into NSData

I have an NSImage which I am trying to resize like so;
NSImage *capturePreviewFill = [[NSImage alloc] initWithData:previewData];
NSSize newSize;
newSize.height = 160;
newSize.width = 120;
[capturePreviewFill setScalesWhenResized:YES];
[capturePreviewFill setSize:newSize];
NSData *resizedPreviewData = [capturePreviewFill TIFFRepresentation];
resizedCaptureImageBitmapRep = [[NSBitmapImageRep alloc] initWithData:resizedPreviewData];
saveData = [resizedCaptureImageBitmapRep representationUsingType:NSJPEGFileType properties:nil];
[saveData writeToFile:#"/Users/ricky/Desktop/Photo.jpg" atomically:YES];
My first issue is that my image gets squashed when I try to resize it and don't conform to the aspect ratio. I read that using -setScalesWhenResized would resolve this problem but it didn't.
My second issue is that when I try to write the image to a file, the image isn't actually resized at all.
Thanks in advance,
Ricky.
I found this blog post to be very helpful for resizing my image: http://weblog.scifihifi.com/2005/06/25/how-to-resize-an-nsimage/
You will need to enforce the aspect ratio on the image resizing yourself, it won't be done for you. This is how I did it when I was trying to fit the image into the printable area on the default paper:
NSImage *image = ... // get your image
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSSize paperSize = printInfo.paperSize;
CGFloat usablePaperWidth = paperSize.width - printInfo.leftMargin - printInfo.rightMargin;
CGFloat resizeWidth = usablePaperWidth;
CGFloat resizeHeight = usablePaperWidth * (image.size.height / image.size.width);
Here is a slightly modified version of his code from the blog:
NSData *sourceData = [image TIFFRepresentation];
float resizeWidth = ... // your desired width;
float resizeHeight = ... // your desired height;
NSImage *sourceImage = [[NSImage alloc] initWithData: sourceData];
NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(resizeWidth, resizeHeight)];
NSSize originalSize = [sourceImage size];
[resizedImage lockFocus];
[sourceImage drawInRect: NSMakeRect(0, 0, resizeWidth, resizeHeight) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
[resizedImage unlockFocus];
NSData *resizedData = [resizedImage TIFFRepresentation];
[sourceImage release];
[resizedImage release];
If you can require Mac OS X 10.6 or later, send your image a CGImageForProposedRect:context:hints: message, then write the CGImage out using a CGImageDestination object.
The rectangle should have NSZeroPoint as its origin, and its size be the size you want.
This still won't scale the image proportionally (maintaining aspect ratio); you have to do that yourself.
The pre-10.6 way to do this (without going through a TIFF representation) is to lock focus on the resized image, create an NSBitmapImageRep for the extent of the image (that is, a rectangle with zero origin and the image's size), unlock focus, and then ask that bitmap image rep for JPEG data.

Setting color when drawing into a NSImage

I'm trying to tag a picture file with a date in Cocoa and are trying to do this in a small command line tool. It works fine...but, I can't seem to be able to set the color. Am I doing something wrong?
#import <Cocoa/Cocoa.h>
int main (int argc, const char * argv[]) {
[NSApplication sharedApplication];
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:
[NSString stringWithFormat:#"%s", "/some/file.png"]];
if (image) {
[image lockFocus];
NSColor *color = [NSColor whiteColor];
// THESE DOESN'T SEEM TO WORK...
[color set];
[color setStroke];
[color setFill];
NSString *string = [NSString stringWithFormat:#"%#", [NSDate date]];
[string drawAtPoint:NSMakePoint(10, 10) withAttributes:nil];
[image unlockFocus];
NSBitmapImageRep *bits = [NSBitmapImageRep imageRepWithData:
[image TIFFRepresentation]];
NSData *data = [bits representationUsingType:NSPNGFileType
properties:nil];
[data writeToFile:#"/some/file.png"
atomically:NO];
}
[pool drain];
return 0;
}
I believe you need to set the color of text using the NSForegroundColorAttributeName attribute in the drawAtPoint:withAttributes: call, rather than setStroke/setFill.

Resources