iPhone - Save UIImage to desktop on simulator

时光总嘲笑我的痴心妄想 提交于 2019-11-28 09:07:04

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

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

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?

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