然后实现委托的 imagePickerController: did FinishPickingMediaWithInfo: 方法,在这里你可以从图像选择器获取照片并保存它(当然,你可能有另一个类/对象来处理保存,但我只是显示方法中的代码) :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// get the captured image
UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *imageSubdirectory = [documentsDirectory stringByAppendingPathComponent:@"MySubfolderName"];
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.png"];
// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to PNG spec
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
}
如果你想保存为 JPEG 图像,最后3行应该是:
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.jpg"];
// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to JPG spec
NSData *imageData = UIImageJPEGRepresentation(image, 0.85f); // quality level 85%
[imageData writeToFile:filePath atomically:YES];