如何在iPhone设备照片库中保存摄像头的捕获图像?

时间:2010-03-04 15:16:12

标签: iphone

我正在开展一个项目,我必须在iPhone设备照片库中保存捕获的图像。

我想问一下如何通过代码实现这一点。下面我粘贴我的代码: -

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

[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
imgglobal =imageView.image;
NSString *newFilePath = [NSHomeDirectory() stringByAppendingPathComponent: @"~/Users/abc/Library/Application Support/iPhone Simulator/User/Applications/SAVE_IMAGE_TEST1.JPG"]; 
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 1.0);
NSData  *data = imageData;
if (imageData != nil) {
    [imageData writeToFile:newFilePath atomically:YES];
}
if ([[NSFileManager defaultManager] createFileAtPath:@"~/Users/abc/Library/Application Support/iPhone Simulator/User/Applications/SAVE_IMAGE_TEST1.JPG" contents:data attributes:nil])
{
    UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image was successfully saved to the Photo Library." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [successAlert show];
    [successAlert release];
} else {
    UIAlertView     *failureAlert = [[UIAlertView alloc] initWithTitle:@"Failure" message:@"Failed to save image to the Photo Library." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [failureAlert show];
    [failureAlert release];
}       

}

但我的应用程序显示失败警报,请将正确的代码发送给我,以将捕获的图像保存到Iphone设备照片库中。

先谢谢其非常紧急的

1 个答案:

答案 0 :(得分:1)

这是一个单行(不计算错误处理)。只需致电UIImageWriteToSavedPhotosAlbum()