xcode - 如何在 Xcode 中保存和加载图片

标签 xcode save load

如果您不太明白,请检查我的应用程序(快速注释!)但它就在这里。我的应用程序是一个笔记应用程序,因此它允许用户从下面几种不同类型的笔记颜色和设计中进行选择。当用户选择一个时,它会将上面的注释更改为他们设置的内容。所以我需要一个按钮来保存他们选择的图片,当离开 View 并返回时,他们可以单击加载按钮,他们选择的相同图像将会出现。我使用的是 Xcode 4.3。

最佳答案

NSImageView 就是您所寻找的。

代码:

-(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/

相关文章:

调试 save() 返回 false CakePHP 3.0

performance - 如何开始性能测试

performance - 如何对以下用例执行负载测试

objective-c - 如何更改 Swift 类的命名空间?

ios - 清除和退出按钮

VBA SaveAs 保存为文件类型而不是 .xlsm

python - Django - 使用 xhtml2pdf 将生成的 PDF 保存在模型中

javascript - 如何防止在 jQuery 函数中重复?

ios - 使用 GoogleService-Info.plist 配置适用于 iOS 应用程序的 Google 分析时出错

ios - Swift-获取自计时器启动以来经过了多少秒/毫秒