iPhone - 将 UIImage 保存到模拟器上的桌面

发布于 2024-11-03 11:02:05 字数 89 浏览 1 评论 0原文

我目前正在开发模拟器。 我想将 UIImage 保存到桌面上的 jpg 文件中。

看来我的路径或其他问题有问题。

感谢您的帮助 :)

I'm currently working on the simulator.
I'd like to save an UIImage to a jpg file, on my desktop.

It seems that I have a problems with paths or something.

Thank you for your help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

固执像三岁 2024-11-10 11:02:05

下面应该帮助您开始... myImage 是一个 UIImage 对象。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *myImageData = UIImagePNGRepresentation(myImage);
[fileManager createFileAtPath:@"/Users/Me/Desktop/myimage.png" contents:myImageData attributes:nil];

编辑:刚刚注意到您想要 JPG 图像,您可以使用 UIImageJPEGRepresentation() 所以:

NSData *myImageData = UIImageJPEGRepresentation(myImage, 1.0);

注意: 这对于快速和肮脏的测试很有用,但我建议学习如何写入文档您的应用程序的目录,然后使用 Finder 查找那里的文件。模拟器中应用程序文档目录的示例路径为:

/Users/Me/Library/Application Support/iPhone Simulator/4.3/Applications/1E2C2C81-1ED6-4DC9-CDD0-CA73FB56501F/Documents

The following should get you started... myImage is an UIImage object.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *myImageData = UIImagePNGRepresentation(myImage);
[fileManager createFileAtPath:@"/Users/Me/Desktop/myimage.png" contents:myImageData attributes:nil];

Edit: Just noticed you wanted a JPG image, you can get the NSData representation of a JPG using UIImageJPEGRepresentation() so:

NSData *myImageData = UIImageJPEGRepresentation(myImage, 1.0);

Note: This is useful for quick and dirty testing but I would suggest learning how to write to the documents directory of your app and then using Finder to find the files there. An example path to an application's document directory in the simulator is:

/Users/Me/Library/Application Support/iPhone Simulator/4.3/Applications/1E2C2C81-1ED6-4DC9-CDD0-CA73FB56501F/Documents

錯遇了你 2024-11-10 11:02:05

尝试转到 cd /Users/{您的用户名}/Library/Application\ Support/iPhone\ Simulator/4.3/Applications/{application id}/Documents/

替换 {您的用户名} 是您的 mac 用户名,{application id} 是您的应用程序 GUID(随机)。您还可以将 4.3 更改为您的应用所针对的版本

Try going to cd /Users/{your username}/Library/Application\ Support/iPhone\ Simulator/4.3/Applications/{application id}/Documents/

Replace {your username} with your mac username and {application id} is your application GUID (random). You also may change 4.3 to what ever version your app is built for

心舞飞扬 2024-11-10 11:02:05

对于Swift

func outputImage(name:String,image:UIImage){
    let fileManager = FileManager.default
    let data = UIImagePNGRepresentation(image)
    fileManager.createFile(atPath: "/Users/UserName/Projects/MyProject/testImages/\(name)", contents: data, attributes: nil)
}

像这样调用,

outputImage(name: "imageName.png", image: image)

For Swift,

func outputImage(name:String,image:UIImage){
    let fileManager = FileManager.default
    let data = UIImagePNGRepresentation(image)
    fileManager.createFile(atPath: "/Users/UserName/Projects/MyProject/testImages/\(name)", contents: data, attributes: nil)
}

Call like this,

outputImage(name: "imageName.png", image: image)
债姬 2024-11-10 11:02:05

该模拟器模拟沙盒应用程序,但由于它位于 Mac 上,因此您可以访问存储该应用程序及其所有文件的目录。因此,在您的应用程序中,如果您可以弄清楚如何将数据写入文件(在应用程序的沙箱内),那么您可以使用 Finder 来定位目录并找到文件。这有帮助还是我错过了什么?

The simulator emulates a sandboxed application, but since it's on your Mac, you can access the directory in which the application and all its files are stored. So, in your app, if you can figure out how to write the data to a file (within the app's sandbox), then you can use the Finder to locate the directory and locate the file. Does this help or am I missing something?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文