iPhone - Save UIImage to desktop on simulator
I'm currently working on the simulator. I'开发者_Python百科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 :)
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
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)
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?
精彩评论