Take the 2-minute tour ×

im trying to merge 2 different images in Cocoa and like to save the resulting image in my application. What I've done so far is this:

-(void)mergeImage:(NSImage*)target withImage:(NSImage*)source {
    [target lockFocus];

    NSPoint aPoint;
    aPoint.x = 1;
    aPoint.y = 1;
    [source drawAtPoint:aPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    [target unlockFocus];

    NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:[target TIFFRepresentation]];

    [target addRepresentation:bmpImageRep];

    NSData *data = [bmpImageRep representationUsingType: NSPNGFileType
                                             properties: nil];

    NSURL* aURL = [[NSBundle mainBundle] bundleURL];

    NSString *tmpPathToFile = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/tempImage.png", aURL]];
    [data writeToFile:tmpPathToFile atomically:YES];

I don't get any error, the urls to the bundle seem to be correct. And the "data" holds ~2,9MB 2985448

share|improve this question
You are not (or should not be) allowed to to write to the application bundle. They're sealed for a reason –  CodaFi Dec 22 '12 at 21:08
add comment

1 Answer

up vote 2 down vote accepted
  1. Is your app sandboxed? If so make sure it does allow writing to the directory you're writing to.
  2. Is there already a file with the same name? atomically won't overwrite it.
  3. Is the file path correct?

Just like CodaFi mentioned it, you are not allowed to write to the application bundle. You can, however, write to the application support folder. That's what it's there for.

share|improve this answer
Oh, ok. That means also temporary data has to go into the application support folder? –  mightym Dec 22 '12 at 21:19
@mightym It depends on what you call temporary. If it's really temporary, which can be deleted after the computer shuts down, there is a special temporary directory you can use. Using NSTemporaryDirectory();. Or if it's something your app will use later, you'll want to save it in the application support folder. –  NSAddict Dec 22 '12 at 21:24
Ah perfect! i had no idea that sth. like NSTemporaryDirectory exists. Thank you very much for! –  mightym Dec 22 '12 at 21:33
add comment

Your Answer


