Exporting unshown NSViewController to JPG - objective-c

I am working on an desktop app that generates an image from en NSViewController that is not shown.
The view generated has 3 NSImageViews which change the content once created.
I manage to export the NSViewController's view to JPG file, but it never renders in the image given to the NSImageView, it keeps the image which is set in the nib. Also, if I don't set the background color to the view's layer... I get an empty result.
What am I missing here? I guess I need to update the NSViewController's view... but how can I do that? I do not want to show my NSViewController.
My code:
- (IBAction)doExport:(id)sender {
ExportView *view = [[ExportView alloc] initWithNibName:#"ExportView" bundle:nil];
[view.pic1 setImage:self.pic1.image];
[view.pic2 setImage:self.pic2.image];
[view.pic3 setImage:self.pic3.image];
[view.icon setImage:self.icon.image];
// Hardcoding for test
[view.pic1 setImage:[NSImage imageNamed:#"iPhone 4-Inch Screenshot 1"]];
[view.pic1 setNeedsLayout:YES];
[view.pic1 setNeedsDisplay];
[view.pic1 updateLayer];
// Setup the image to render
NSRect imgRect = view.view.frame;
NSSize imgSize = imgRect.size;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:imgSize.width
pixelsHigh:imgSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0];
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
// Set view background color
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:[NSColor clearColor].CGColor];
[view.view setWantsLayer:YES];
[view.view setLayer:viewLayer];
// Render
CGContextRef zCgContextRef = (CGContextRef) [g graphicsPort];
[[view.view layer] renderInContext:zCgContextRef];
// Render pic as well, for test
[view.pic1.layer renderInContext:zCgContextRef];
// Get the Data for the image
NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];
// Start the savepanel
NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = #"Save chart";
[savepanel setAllowedFileTypes:[NSArray arrayWithObject:#"jpg"]];
[savepanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result)
{
if (NSFileHandlingPanelOKButton == result)
{
NSURL* fileURL = [savepanel URL];
if ([fileURL.pathExtension isEqualToString:#""])
fileURL = [fileURL URLByAppendingPathExtension:#"jpg"];
[exportedData writeToURL:fileURL atomically:YES];
}
}];
}

I solved it in a very ugly way. Basically what I do is I add the view to my contentView and set setHidden to YES straight away. This will intialize the view. Now I change the content of my Nib view.
When its time to render the data to NSData I change setHidden to NO, generate the NSData and then remove the view.
The end result (no beautiful code :( ):
- (IBAction)doExport:(id)sender {
ExportView *view = [[ExportView alloc] initWithNibName:#"ExportView" bundle:nil];
[view.view setHidden:YES];
[self.window.contentView addSubview:view.view];
[view.pic1 setImage:self.pic1.image];
[view.pic2 setImage:self.pic2.image];
[view.pic3 setImage:self.pic3.image];
[view.icon setImage:self.icon.image];
// Setup the image to render
NSRect imgRect = view.view.frame;
NSSize imgSize = imgRect.size;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:imgSize.width
pixelsHigh:imgSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0];
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
// Set view background color
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:[NSColor clearColor].CGColor];
[view.view setWantsLayer:YES];
[view.view setLayer:viewLayer];
[view.view setHidden:NO];
// Render
CGContextRef zCgContextRef = (CGContextRef) [g graphicsPort];
[[view.view layer] renderInContext:zCgContextRef];
[view.view removeFromSuperview];
// Get the Data for the image
NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];
// Start the savepanel
NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = #"Save chart";
[savepanel setAllowedFileTypes:[NSArray arrayWithObject:#"jpg"]];
[savepanel setNameFieldStringValue:#"Test"];
[savepanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result)
{
if (NSFileHandlingPanelOKButton == result)
{
NSURL* fileURL = [savepanel URL];
if ([fileURL.pathExtension isEqualToString:#""])
fileURL = [fileURL URLByAppendingPathExtension:#"jpg"];
[exportedData writeToURL:fileURL atomically:YES];
}
}];
}

Related

Is there any way to Convert NSView Contain Subviews into NSImage?

Is there any way to Convert NSView Contain many Subviews with background color into NSImage ?
Sample code will be great
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[_collageView bounds]];
[_collageView unlockFocus];
// NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];
NSData *exportedData = [rep representationUsingType:NSPNGFileType properties:nil];
NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = #"Save chart";
[savepanel setAllowedFileTypes:[NSArray arrayWithObject:#"png"]];
[savepanel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
{
if (NSFileHandlingPanelOKButton == result)
{
NSURL* fileURL = [savepanel URL];
if ([fileURL.pathExtension isEqualToString:#""])
fileURL = [fileURL URLByAppendingPathExtension:#"png"];
[exportedData writeToURL:fileURL atomically:YES];
}
}];
i also used the following code to
[_collageView lockFocus];
NSBitmapImageRep *bits = [[NSBitmapImageRep alloc]initWithFocusedViewRect: [_collageView bounds]];
[_collageView unlockFocus];
NSImage *image1 = [[NSImage alloc] init];
[image1 addRepresentation: bits];
[self.ImageView setImage:image1];
but not working
Your approach copies the bitmap from the windows server. This is the rendered content of the view. Typically you do not want to do that.
It is a better way to lock the focus on an NSImage, which builds up the graphic contexts and then draw into that image using -drawRect: or whatever drawing methods you have. You can add subviews by simply iterating them.
// perhaps in a category of NSView
- (void)drawRecursively
{
[self drawRect:self.bounds];
for (NSView *subview in self.subviews)
{
[subview drawRecursively];
}
}
To start that
NSImage *image = [[NSImage alloc] initWithSize:theView.bounds.size];
[image lockFocus]; // Set-up the context for the image
[theView drawRecursively];
[image unlockFocus];
Typped in Safari.
Edit: Before going to the subviews, you have to perform a transformation for them. Forgot that.
I have actually implemented the drawing of a view hierarchy offscreen myself.
It is non-trivial, even for a very simple view hierarchy where all views are
subclasses NSControl and have -(BOOL) isFlipped { return YES; }
Here's one small portion of that code, which will give you an idea of the pain of doing this:
- (void)drawRect:(NSRect)iDirtyBigRect
{
#ifdef DISABLE_PSEUDOVIEW
debugRectsBeingDrawn();
return;
#endif
if ( !drawableShape )
{
if ( [[[self window] contentView] wantsLayer] )
{
CGRect cgRect = NSRectToCGRect([self bounds]);
drawableShape = HIShapeCreateMutableWithRect( &cgRect );
}
else
{
DUMP( #"our superview should have set up our drawableShape!" );
[self setNeedsDisplay:YES];
return;
}
}
else if ( iDirtyBigRect.size.width <= 0 )
{
// Our drawable shape is empty.
// This can happen for several valid reasons:
// 1) dirtyShape is fully masked by opaqueShape
// 2) for some reason, we were called to obey needsDisplay when not needed
DLog( #"nothing to draw %#", self );
return; // must return, otherwise we risk drawing outside our bounds
}
//NSArray *hardSubviews = [self subviews];
for ( NSView *pseudoSubview in pseudoSubviews )
{
if ( ![pseudoSubview isHidden] &&
[pseudoSubview window] == offscreenWindow ) // only draw pseudo view if it's in offscreen window
{
NSRect rectToDraw = [pseudoSubview frame];
// if ( rectToDraw.size.width == 0 )
// {
// DLog( #"clipped to superview %#", pseudoSubview );
// }
CGRect cgRect = NSRectToCGRect(rectToDraw);
if ( HIShapeIntersectsRect(drawableShape, &cgRect) )
{
// the magic: transform to subview coordinates
NSAffineTransform* xform = [NSAffineTransform transform];
[xform translateXBy:rectToDraw.origin.x yBy:rectToDraw.origin.y];
if ( [pseudoSubview isFlipped] != [self isFlipped] )
{
if ( ![pseudoSubview isKindOfClass: [NSColorWell class]] )
{
DUMP( #"hmmm flippedness different %d %d", [pseudoSubview isFlipped], [self isFlipped] );
}
[xform translateXBy:0.0 yBy:rectToDraw.size.height];
[xform scaleXBy:1.0 yBy:-1.0];
}
[xform concat];
HIMutableShapeRef newShape = HIShapeCreateMutableWithRect( &cgRect );
HIShapeIntersect( newShape, drawableShape, newShape ); // clip to subview frame
HIShapeOffset( newShape, -cgRect.origin.x, -cgRect.origin.y ); // translate to subview coords
CGRect dirtyRect;
HIShapeGetBounds(newShape, &dirtyRect);
// if ( dirtyRect.size.width <= 0 )
// {
// DUMP( #"why?" );
// }
if ( [pseudoSubview isKindOfClass:[PseudoView class]] )
{
//DLog( #"drawing Pseudo %# dirtyRect %# ", pseudoSubview, NSStringFromRect(NSRectFromCGRect(dirtyRect)));
PseudoView *pView = (PseudoView *)pseudoSubview;
if ( pView->drawableShape )
{
CFRelease( pView->drawableShape );
pView->drawableShape = NULL;
}
// create subview personal drawableShape from my drawable shape
pView->drawableShape = newShape;
if ( [pseudoSubview isOpaque] )
gDrawDirtyPixels += dirtyRect.size.width * dirtyRect.size.height;
if ( dirtyRect.size.width > 0 )
[pseudoSubview drawRect: NSRectFromCGRect(dirtyRect)];
}
else
{
//DLog( #"drawing non-Pseudo %# rectToDraw %# ", pseudoSubview, NSStringFromRect( rectToDraw ));
UInt64 t1 = CpuCycles();
CFRelease( newShape );
// sacrifice efficiency to avoid bugs...
// always draw the entire view.
[pseudoSubview drawRect:[pseudoSubview bounds]]; // NSRectFromCGRect(dirtyRect)]; // [pseudoSubview bounds]];
UnitTestQuartz( [pseudoSubview bounds] );
// NSLog( #"drawRect %# %# sUnitTestQuartzToggle %f\n -- superv %#\n -- self %#", pseudoSubview, NSStringFromRect(rectToDraw), sUnitTestQuartzToggle*100, [pseudoSubview superview], self );
UInt64 t2 = CpuCycles();
int diff = (int)(t2-t1);
gDrawControl += diff;
}
[xform invert]; // restore
[xform concat];
}
}
}
//if ( [self isKindOfClass:[PseudoRootView class]] )
// [offscreenWindow setViewsNeedDisplay:NO];
}
I am able to solve my problem with the following Code
// Setup the image to render
NSRect imgRect = _collageView.frame;
NSSize imgSize = imgRect.size;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:imgSize.width
pixelsHigh:imgSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0];
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:context];
// Render
CGContextRef zCgContextRef = (CGContextRef) [context graphicsPort];
[[_collageView layer] renderInContext:zCgContextRef];
// Get the Data for the image
NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];
// Start the savepanel
NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = #"Save chart";
[savepanel setAllowedFileTypes:[NSArray arrayWithObject:#"jpg"]];
[savepanel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
{
if (NSFileHandlingPanelOKButton == result)
{
NSURL* fileURL = [savepanel URL];
if ([fileURL.pathExtension isEqualToString:#""])
fileURL = [fileURL URLByAppendingPathExtension:#"jpg"];
[exportedData writeToURL:fileURL atomically:YES];
}
}];

How can I save the image from IKImageView?

Okay... I want to save the visible rectangle of the IKImageView image only.
My problem is that, somehow, if I had an image in portrait mode it will be not saved in the right orientation. I drawing the image with this code:
[sourceImg drawInRect:targetRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];
On the other side if I draw the image with this code:
[sourceImg drawAtPoint:NSZeroPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];
It will be in the right orientation, but edits like zooming are lost. I mean it will save the right cut out, but unfortunately not in the right size.
Here is the code how I load the image:
- (IBAction)imageSelectionButtonAction:(id)sender {
NSLog(#"%s", __FUNCTION__);
NSOpenPanel *panel = [NSOpenPanel openPanel];
id model = [self getModel];
if (imageView) {
[panel setAllowedFileTypes:[NSImage imageFileTypes]];
[panel beginSheetModalForWindow:[[NSApplication sharedApplication] mainWindow] completionHandler:^(NSInteger returnCode) {
if (returnCode == 1) {
NSURL *imageUrl = [panel URL];
CGImageRef image = NULL;
CGImageSourceRef isr = CGImageSourceCreateWithURL( (__bridge CFURLRef)imageUrl, NULL);
if (isr) {
NSDictionary *options = [NSDictionary dictionaryWithObject: (id)kCFBooleanTrue forKey: (id) kCGImageSourceShouldCache];
image = CGImageSourceCreateImageAtIndex(isr, 0, (__bridge CFDictionaryRef)options);
if (image) {
_imageProperties = (__bridge_transfer NSDictionary*)CGImageSourceCopyPropertiesAtIndex(isr, 0, (__bridge CFDictionaryRef)_imageProperties);
_imageUTType = (__bridge NSString*)CGImageSourceGetType(isr);
}
CFRelease(isr);
}
if (image) {
[imageView setImage:image imageProperties:_imageProperties];
CGImageRelease(image);
}
[[model saveTempMutabDict] setValue:[imageUrl absoluteString] forKey:#"tempImage"];
}
}];
return;
}
}
And here is the code how I save it:
- (void)saveImage:(NSString *)path {
// get the current image from the image view
CGImageRef sourceImgRef = [imageView image];
NSRect targetRect = [imageView visibleRect];
NSImage *sourceImg = [[NSImage alloc] initWithCGImage:sourceImgRef size:NSZeroSize];
NSMutableDictionary *thisTiffDict = [_imageProperties valueForKey:#"{TIFF}"];
NSInteger theOrientation = [[thisTiffDict valueForKey:#"Orientation"] integerValue];
NSImage *targetImg = nil;
if (theOrientation == 6) {
targetImg = [[NSImage alloc] initWithSize:NSMakeSize([imageView frame].size.height, [imageView frame].size.width)];
} else {
targetImg = [[NSImage alloc] initWithSize:NSMakeSize([imageView frame].size.width, [imageView frame].size.height)];
}
NSRect sourceRect = [imageView convertViewRectToImageRect:targetRect];
[targetImg lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[sourceImg drawAtPoint:NSZeroPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];
[targetImg unlockFocus];
_saveOptions = [[IKSaveOptions alloc] initWithImageProperties:_imageProperties imageUTType: _imageUTType];
NSString * newUTType = [_saveOptions imageUTType];
CGImageRef targetImgRef = [targetImg CGImageForProposedRect:NULL context:[NSGraphicsContext currentContext] hints:nil];
if (targetImgRef) {
NSURL *url = [NSURL fileURLWithPath: path];
CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, (__bridge CFStringRef)newUTType, 1, NULL);
if (dest) {
CGImageDestinationAddImage(dest, targetImgRef, (__bridge CFDictionaryRef)[_saveOptions imageProperties]);
CGImageDestinationFinalize(dest);
CFRelease(dest);
}
}
}
I have no idea what I'm doing wrong?
Thank you
Jens

Is it possible to change the header height of an NSTextView?

I am saving a PDF from an NSTextview and putting a logo in the header. I overrode pageHeader and the logo appears but it is clipped.
Is it possible to change the header height of an NSTextView?
Thanks!
Partial code:
-(IBAction)impLaudo:(id)sender
{
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:[[dirLaudos stringByAppendingString:[estudo stringValue]] stringByAppendingString:#".pdf"] forKey:NSPrintSavePath];
printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
[printInfo setHorizontalPagination: NSClipPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];
[[printInfo dictionary] setValue:[NSNumber numberWithBool:YES] forKey:NSPrintHeaderAndFooter];
printOp = [NSPrintOperation printOperationWithView:textView printInfo:printInfo];
[printOp setShowsPrintPanel:NO];
[printOp runOperation];
}
#implementation MyTextView
- (NSAttributedString *)pageHeader
{
// Adicionando cabeçalho
NSAttributedString *theHeader = nil;
NSImage * pic = [[NSImage alloc] initWithContentsOfFile:[dirLayout stringByAppendingString:#"cabecalho.jpg"]];
NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setAttachmentCell: attachmentCell ];
theHeader = [NSAttributedString attributedStringWithAttachment: attachment];
return theHeader;
}
#end
Instead of overriding -pageHeader, you should override -drawPageBorderWithSize:, which allows you to draw additional marks on the page at print time.
The Size parameter is an NSSize struct containing the size of the current logical page. All you need to do is draw your logo in the correct location:
- (void)drawPageBorderWithSize:(NSSize)pageSize
{
[super drawPageBorderWithSize:pageSize];
//draw your logo
NSPoint offset = NSMakePoint(100.0, 100.0);
NSImage* logo = [NSImage imageNamed:#"logo"];
NSSize logoSize = [logo size];
NSPoint imageOrigin = NSMakePoint(offset.x, pageSize.height - (offset.y + logoSize.height));
[self lockFocus];
[logo drawInRect:NSMakeRect(imageOrigin.x, imageOrigin.y, logoSize.width, logoSize.height)
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0
respectFlipped:YES
hints:nil];
[self unlockFocus];
}

Lazy loading of PhotoLibrary Images

i found an issue with Photo Library Images. It not displaying first time in my View,Image View is blank while loading first time.
Because i found Asset Library block working on another thread.After reloading my View ,I can see all the Images. However first time the Image Views are Blank.
can any one tell me a good way to deal with the problem
It working with Bundle Images.
also some times console shows that
app is crashing due to Program received signal: “0”. Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")
My Code:
for (int j = 0; j<9; j++)
{
//allocating View
UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(xCordImage, yCordImage, 200, 190)];
// allocating ImageView
imageViewTopic = [[[UIImageView alloc] init] autorelease];
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *images;
if (iref) {
images = [UIImage imageWithCGImage:iref];
}
else {
images = [UIImage imageNamed:#"Nofile.png"];
}
imageViewTopic .image = images ;
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
imageViewTopic .image = [UIImage imageNamed:#"Nofile.png"];
NSLog(#"booya, cant get image - %#",[myerror localizedDescription]);
};
NSString *string ;
MyClass *obj = [imageFileNameArray objectAtIndex:j];
**//obj.fileName contains ALAsset URL of a Image**
string = obj.fileName;
NSURL *asseturl = [NSURL URLWithString:string];
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:asseturl resultBlock:resultblock
failureBlock:failureblock];
imageViewTopic.userInteractionEnabled = YES;
imageViewTopic.frame = CGRectMake(0,0, 200, 150);
[currentView addSubview:scroller];
**// adding the imageView to View**
[smallView addSubview:imageViewTopic];
[myView addSubview:smallView];
[scroller addSubview:myView];
}
I am using this method to show images in scroll view with lazy loading. It works well.
First initialize the value of j1. And data is the image data coming from loop from an array.
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL:url];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
__block int j1=_j;
// WARNING: is the cell still using the same data by this point??
// NSURL *url = [NSURL URLWithString: imageName];
UIImage *image = [UIImage imageWithData: data]; //image.size.height
image1=[[UIImageView alloc] initWithFrame:CGRectMake(j1,10,image.size.width,image.size.height)];
image1.image=image;
CALayer *layer = [image1 layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:0.0]; //note that when radius is 0, the border is a rectangle
[layer setBorderWidth:3.0];
[layer setBorderColor:[[UIColor whiteColor] CGColor]];
[portfolio_scroll addSubview:image1];
});
});
_j = _j+ 320;

Display NSImage on a CALayer

I've been trying to display a NSImage on a CALayer. Then I realised I need to convert it to a CGImage apparently, then display it...
I have this code which doesn't seem to be working
CALayer *layer = [CALayer layer];
NSImage *finderIcon = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kFinderIcon)];
[finderIcon setSize:(NSSize){ 128.0f, 128.0f }];
CGImageSourceRef source;
source = CGImageSourceCreateWithData((CFDataRef)finderIcon, NULL);
CGImageRef finalIcon = CGImageSourceCreateImageAtIndex(source, 0, NULL);
layer.bounds = CGRectMake(128.0f, 128.0f, 4, 4);
layer.position = CGPointMake(128.0f, 128.0f);
layer.contents = finalIcon;
// Insert the layer into the root layer
[mainLayer addSublayer:layer];
Why? How can I get this to work?
From the comments: Actually, if you're on 10.6, you can also just set the CALayer's contents to an NSImage rather than a CGImageRef...
If you're on OS X 10.6 or later, take a look at NSImage's CGImageForProposedRect:context:hints: method.
If you're not, I've got this in a category on NSImage:
-(CGImageRef)CGImage
{
CGContextRef bitmapCtx = CGBitmapContextCreate(NULL/*data - pass NULL to let CG allocate the memory*/,
[self size].width,
[self size].height,
8 /*bitsPerComponent*/,
0 /*bytesPerRow - CG will calculate it for you if it's allocating the data. This might get padded out a bit for better alignment*/,
[[NSColorSpace genericRGBColorSpace] CGColorSpace],
kCGBitmapByteOrder32Host|kCGImageAlphaPremultipliedFirst);
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:bitmapCtx flipped:NO]];
[self drawInRect:NSMakeRect(0,0, [self size].width, [self size].height) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapCtx);
CGContextRelease(bitmapCtx);
return (CGImageRef)[(id)cgImage autorelease];
}
I think I wrote this myself. But it's entirely possible that I ripped it off from somewhere else like Stack Overflow. It's an older personal project and I don't really remember.
Here's some code which may help you - I sure hope the formatting of this does not get all messed up like it appears is going to happen - all I can offer is that this works for me.
// -------------------------------------------------------------------------------------
- (void)awakeFromNib
{
// setup our main window 'contentWindow' to use layers
[[contentWindow contentView] setWantsLayer:YES]; // NSWindow*
// create a root layer to contain all of our layers
CALayer *root = [[contentWindow contentView] layer];
// use constraint layout to allow sublayers to center themselves
root.layoutManager = [CAConstraintLayoutManager layoutManager];
// create a new layer which will contain ALL our sublayers
// -------------------------------------------------------
mContainer = [CALayer layer];
mContainer.bounds = root.bounds;
mContainer.frame = root.frame;
mContainer.position = CGPointMake(root.bounds.size.width * 0.5,
root.bounds.size.height * 0.5);
// insert layer on the bottom of the stack so it is behind the controls
[root insertSublayer:mContainer atIndex:0];
// make it resize when its superlayer does
root.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
// make it resize when its superlayer does
mContainer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
}
// -------------------------------------------------------------------------------------
- (void) loadMyImage:(NSString*) path
n:(NSInteger) num
x:(NSInteger) xpos
y:(NSInteger) ypos
h:(NSInteger) hgt
w:(NSInteger) wid
b:(NSString*) blendstr
{
#ifdef __DEBUG_LOGGING__
NSLog(#"loadMyImage - ENTER [%#] num[%d] x[%d] y[%d] h[%d] w[%d] b[%#]",
path, num, xpos, ypos, hgt, wid, blendstr);
#endif
NSInteger xoffset = ((wid / 2) + xpos); // use CORNER versus CENTER for location
NSInteger yoffset = ((hgt / 2) + ypos);
CIFilter* filter = nil;
CGRect cgrect = CGRectMake((CGFloat) xoffset, (CGFloat) yoffset,
(CGFloat) wid, (CGFloat) hgt);
if(nil != blendstr) // would be equivalent to #"CIMultiplyBlendMode" or similar
{
filter = [CIFilter filterWithName:blendstr];
}
// read image file via supplied path
NSImage* theimage = [[NSImage alloc] initWithContentsOfFile:path];
if(nil != theimage)
{
[self setMyImageLayer:[CALayer layer]]; // create layer
myImageLayer.frame = cgrect; // locate & size image
myImageLayer.compositingFilter = filter; // nil is OK if no filter
[myImageLayer setContents:(id) theimage]; // deposit image into layer
// add new layer into our main layer [see awakeFromNib above]
[mContainer insertSublayer:myImageLayer atIndex:0];
[theimage release];
}
else
{
NSLog(#"ERROR loadMyImage - no such image [%#]", path);
}
}
+ (CGImageRef) getCachedImage:(NSString *) imageName
{
NSGraphicsContext *context = [[NSGraphicsContext currentContext] graphicsPort];
NSImage *img = [NSImage imageNamed:imageName];
NSRect rect = NSMakeRect(0, 0, [img size].width, [img size].height);
return [img CGImageForProposedRect:&rect context:context hints:NULL];
}
+ (CGImageRef) getImage:(NSString *) imageName withExtension:(NSString *) extension
{
NSGraphicsContext *context = [[NSGraphicsContext currentContext] graphicsPort];
NSString* imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:extension];
NSImage* img = [[NSImage alloc] initWithContentsOfFile:imagePath];
NSRect rect = NSMakeRect(0, 0, [img size].width, [img size].height);
CGImageRef imgRef = [img CGImageForProposedRect:&rect context:context hints:NULL];
[img release];
return imgRef;
}
then you can set it:
yourLayer.contents = (id)[self getCachedImage:#"myImage.png"];
or
yourLayer.contents = (id)[self getImage:#"myImage" withExtension:#"png"];