2012-03-09 22 views

回答

1

假設您的圖像被稱爲圖像

//Where the 0 denotes the compression (0 to 1). 
NSData *imageData = UIImageJPEGRepresentation(image, 0); 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyImageFolder"]; 

NSError *error; 
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder 

NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", imageName]]; //add our image to the path 

bool successI = [imageData writeToFile:fullPath atomically:YES]; 
if (successI) { 
    //  NSLog(@"Image saved correctly"); 
} else 
    NSLog(@"Error while saving Image"); 

忘了提,你可以改變

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

保存到其他地方。

1

你可以先轉換的UIImage到NSData的由

UIImageJPEGRepresentation 

UIImagePNGRepresentation 

然後將數據文件或庫文件夾寫在你的應用程序。

相關問題