//如果要将图片保存到 Documents中 则用下面这个函数
//并且在紧挨着的下一个函数里加上
//[UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
//这句话
- (NSString *) findUniqueSavePath
{
int i = 1;
NSString *path;
do
{
path = [NSString stringWithFormat:@"%@/Documents/IMAGE_%04d.PNG", NSHomeDirectory(), i++];
} while ([[NSFileManager defaultManager] fileExistsAtPath:path]);
return path;
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CFShow(info);
SETIMAGE([info objectForKey:@"UIImagePickerControllerEditedImage"]);
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
// 告诉委托用户选择了一张图片 这种过时的方法只支持静态图片采集。
//替换方法,imagePickerController:didFinishPickingMediaWithInfo:, 支持采摘电影以及静态图片
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:editingInfo];
[dict setObject:image forKey:@"UIImagePickerControllerEditedImage"];
[self imagePickerController:picker didFinishPickingMediaWithInfo:dict];
}
- (void) pickImage: (id) sender
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
/* UIImagePickerControllerSourceTypePhotoLibrary, // 设备的图片库
UIImagePickerControllerSourceTypeCamera, // 设备的摄像头
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 保存的图像*/
ipc.delegate = self;
ipc.allowsImageEditing = YES;
[self presentModalViewController:ipc animated:YES];
//presentModalViewController显示半透明的一个view
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Pick", @selector(pickImage:));
self.title = @"Image Picker";
}