如何从相册中加载捕获的图像

时间:2014-04-08 08:12:10

标签: ios uiimage uiimagepickercontroller photo-gallery

我有一个捕捉图片的按钮然后保存到photoAlbum。我使用imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;使用了另一个加载图像按钮,然后我实现了didFinishPickingMediaWithInfo:将相册加载到newImage。我的需求是在捕获照片后我有alertview然后单击YES按钮自动加载最后捕获的图​​像PhotoAlbum并添加newImage。有可能吗?

- (void)saveImageToPhotoAlbum
{

    [self.library saveImage:[self  stillImage] toAlbum:@"Album" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];


    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Gallery"
                                                        message:@"Do you want to use the captured image?" delegate:self
                                              cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];


    alertView.tag = 2;
    [alertView show];


}

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


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {



        newImage = [[UIImageView alloc] initWithImage:[info objectForKey:UIImagePickerControllerOriginalImage]];

        [newImage setFrame:CGRectMake(0, 0, 320, 568)];

       [self.view addSubView:newImage];
}

我需要将最后捕获的图​​像显示到newImage,单击alertView按钮:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons

     if (buttonIndex == 1){

}
}

1 个答案:

答案 0 :(得分:0)

 Before use below code take UIImage *img in .h file   

     - (void)saveImageToPhotoAlbum
        {

            [self.library saveImage:[self  stillImage] toAlbum:@"Album" withCompletionBlock:^(NSError *error) {
                if (error!=nil) {
                    NSLog(@"Big error: %@", [error description]);
                }
            }];


            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Gallery"
                                                                message:@"Do you want to use the captured image?" delegate:self
                                                      cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];


            alertView.tag = 2;
            [alertView show];


        }

        -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
            //set image
        img=nil;
        img=[info objectForKey:UIImagePickerControllerOriginalImage];
        [self saveImageToPhotoAlbum];
//        [self dismissViewControllerAnimated:YES completion:nil];
    }

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    {

        if (buttonIndex == 1)
       {

             if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

                        if(newImage)
                        {
                             [newImage removeFromSuperview];
                              newImage=nil;
                        }
                        newImage = [[UIImageView alloc] initWithImage:img];

                        [newImage setFrame:CGRectMake(0, 0, 320, 568)];

                       [self.view addSubView:newImage];
             }
             [picker dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
          img=nil;
          [picker dismissViewControllerAnimated:YES completion:nil];
        }
    }