mightym
mightym

Reputation: 191

Cocoa merge 2 Images and save them

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

Upvotes: 1

Views: 642

Answers (1)

IluTov
IluTov

Reputation: 6862

  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.

Upvotes: 2

Related Questions