2011-07-30 40 views
0

我已經從UIImagePicker thingy中拉出了一張圖像,我想暫時將其保存到沙箱環境中,無論是在文檔文件夾還是其他類似的等效物。但是,我不知道該怎麼做,請有人幫助我嗎?由於如何將圖像保存到沙箱 - iPhone SDK

目前的形象是一個UIImage

+0

見:http://stackoverflow.com/問題/ 5794422/iphone-save-uiimage-to-desktop-on-simulator –

+0

這很好,除了我不想將它保存到桌面上,因爲這不是代碼在設備上運行時很多用途 – Jon

回答

12

保存圖像:

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

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

} 

加載圖像

-(UIImage)loadImage{ 

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

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

謝謝,這真是太棒了 – Jon

+0

如果這解決了您的問題,請接受答案。 – Cyprian

1
-(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]; 

}