如果您不太明白,请检查我的应用程序(快速注释!)但它就在这里。我的应用程序是一个笔记应用程序,因此它允许用户从下面几种不同类型的笔记颜色和设计中进行选择。当用户选择一个时,它会将上面的注释更改为他们设置的内容。所以我需要一个按钮来保存他们选择的图片,当离开 View 并返回时,他们可以单击加载按钮,他们选择的相同图像将会出现。我使用的是 Xcode 4.3。
最佳答案
NSImageView 就是您所寻找的。
- 这包含有关保存文件的信息(使用代码查看答案):Implement drag from NSImageView and save image to a file
代码:
-(IBAction)saveImageButtonPushed:(id)sender
{
NSBitmapImageRep *rep;
NSData *data;
NSImage *image;
[self lockFocus];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self frame]];
[self unlockFocus];
image = [[[NSImage alloc] initWithSize:[rep size]] autorelease];
[image addRepresentation:rep];
data = [rep representationUsingType: NSPNGFileType properties: nil];
//save as png but failed
[data writeToFile: @"asd.png" atomically: NO];
//save as pdf, succeeded but with flaw
data = [self dataWithPDFInsideRect:[self frame]];
[data writeToFile:@"asd.pdf" atomically:YES];
}
//......
@end
加载图像:
代码:
NSImage loadedImage = [[NSImage alloc] initWithContentsOfFile: NSString* filePath]
关于xcode - 如何在 Xcode 中保存和加载图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11316553/