iPhone - Save UIImage to desktop on simulator

不羁岁月 提交于 2019-12-17 18:49:09

问题


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 :)


回答1:


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




回答2:


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




回答3:


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?




回答4:


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)


来源:https://stackoverflow.com/questions/5794422/iphone-save-uiimage-to-desktop-on-simulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!