How do I save an image to the sandbox - iPhone SDK

2019-03-03 18:18发布

问题:

I have pulled an image out of the UIImagePicker thingy, and I would like to temporarily save it to the sandbox environment, in either the documents folder, or some other similar equivalent. However, I do not know how to do this, please could someone help me? Thanks

At the moment the image is a UIImage

回答1:

Saving an image:

-(void)saveImage:(UIImage*)image{
    NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/someImageName.png"];

   [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];

}

Loading an image

-(UIImage)loadImage{

   NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/someImageName.png"];

   UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
}


回答2:

-(void)SaveImage

{

NSData *data;

NSArray *paths;

NSString *documentsDirectory,*imagePath ;

UIImage *image=[UIImage imageNamed:@"public.png"];

 paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

documentsDirectory = [paths objectAtIndex:0];

imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString   stringWithFormat:@"public.png"]];

data = UIImagePNGRepresentation(image);

[data writeToFile:imagePath atomically:YES];

}