将图像从相机保存到文档目录

时间:2013-10-26 08:14:44

标签: ios xcode uiimage uiimagepickercontroller

我有以下代码启动相机应用程序,用户可以选择“使用照片”。然而,没有任何事情发生。

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentModalViewController:imagePickerController animated:YES];
    } 

如何从中获取UIimage并将其保存到相册中。理想情况下,我想将其保存到文档目录中,并知道如何执行该部分。谢谢!

4 个答案:

答案 0 :(得分:3)

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

 UIImage* originalImage = nil;
    originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
    if(originalImage==nil)
    {
        originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    if(originalImage==nil)
    {
        originalImage = [info objectForKey:UIImagePickerControllerCropRect];
    }



    [addImageOutlet setImage:originalImage forState:UIControlStateNormal];
       [self saveBackgroundImageInDocumentDirectory:originalImage];


    [self dismissViewControllerAnimated:YES completion:nil];

}

答案 1 :(得分:2)

使用此代码:

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

    UIImage  *image =  [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData * imageData = UIImagePNGRepresentation(image);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    [imageData writeToFile:savedImagePath atomically:NO];

    [picker dismissViewControllerAnimated:YES completion:nil];
}

答案 2 :(得分:1)

我认为它会对你有帮助,

 //you have to set the delegate which clicking the button of camera

 imagePickerController.delegate = self;


//This Delegate method will call

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

    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage* selectedImage =  [info objectForKey:UIImagePickerControllerOriginalImage];

   NSString *path = [[self pathToPatientPhotoFolder] stringByAppendingPathComponent:[NSString stringWithFormat:@"imageName.png"]];

   NSError * error11 = nil;

   [post.getData writeToFile:path options:NSDataWritingAtomic error:&error11];

}



//This method is useful for the get the Documentry path

    - (NSString *)pathToPatientPhotoFolder {
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                            NSUserDomainMask,
                                                                            YES) lastObject];
        NSString *myalbumpath = [documentsDirectory stringByAppendingPathComponent:@"MYAlbum"];

        // Create the folder if necessary
        BOOL isDir = NO;
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        if (![fileManager fileExistsAtPath:myalbumpath
                               isDirectory:&isDir] && isDir == NO) {
            [fileManager createDirectoryAtPath:myalbumpath
                   withIntermediateDirectories:NO
                                    attributes:nil
                                         error:nil];
        }
        return myalbumpath;
    }

享受编码

答案 3 :(得分:1)

addImageOutlet用于什么类型的对象