2011-06-27 37 views

回答

3

這是用於寫入與用戶指定的名稱

// Create paths to output images 
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"]; 
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; 

// Write a UIImage to JPEG with minimum compression (best quality) 
// The value 'image' must be a UIImage object 
// The value '1.0' represents image compression quality as value from 0.0 to 1.0 
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES]; 

// Write image to PNG 
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES]; 

// Let's check to see if files were successfully written... 

// Create file manager 
NSError *error; 
NSFileManager *fileMgr = [NSFileManager defaultManager]; 

// Point to Document directory 
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

// Write out the contents of home directory to console 
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 
+0

感謝您回覆我的問題。實際上,我需要將照片保存爲具有特定名稱的相冊,而不是文檔目錄。 – Satya

1

影像使用委託UIImagePickerControllerDelegate用於調用照相機或圖像庫中的代碼。

調用攝像頭:

UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 
picker.delegate = self; 

picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
isCamera = YES; 

[self presentModalViewController:picker animated:YES]; 

調用庫:

UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 
picker.delegate = self; 
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary; 

[picker setAllowsEditing:YES]; 

[self presentModalViewController:(UIViewController*)picker animated:YES]; 

一旦完成,委託方法被調用:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

要寫入圖像庫使用:

UIImageWriteToSavedPhotosAlbum(currentImage, nil, nil, nil); 

或將從照片庫中選取的選定圖像保存到UIImage並對其執行進一步的操作。

+0

@JaninAnk:嗨,我知道如何使用imagePicker和保存圖像等,但我的疑問是,如何保存和訪問這些具有特定名稱的圖像。 – Satya

+0

您的程序不允許在「沙箱」之外保存東西。 – JainAnk

相關問題