1

我试图在 Cocoa 中合并 2 个不同的图像,并喜欢将生成的图像保存在我的应用程序中。到目前为止我所做的是:

-(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];
}

我没有收到任何错误,捆绑包的网址似乎是正确的。并且“数据”包含〜2,9MB 2985448

4

1 回答 1

2
  1. 你的应用是沙盒的吗?如果是这样,请确保它确实允许写入您正在写入的目录。
  2. 是否已经存在同名文件?atomically不会覆盖它。
  3. 文件路径是否正确?

就像CodaFi提到的那样,您不能写入应用程序包。但是,您可以写入应用程序支持文件夹。这就是它的用途。

于 2012-12-22T21:06:33.807 回答