简体   繁体   English

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

[英]How to load captured image from photo album

I have a button to capture picture then it save to photoAlbum. 我有一个按钮来捕获图片,然后将其保存到photoAlbum。 I used another button for load image from using imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 我使用imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;使用另一个按钮加载图像imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; then i implemented didFinishPickingMediaWithInfo: for loading photo album to newImage. 然后我实现didFinishPickingMediaWithInfo:用于将相册加载到newImage。 My need is after capture photo i have alertview then click YES button automatically load last captured image PhotoAlbum and add newImage. 我的需要是在捕获照片后,我具有alertview,然后单击“是”按钮,自动加载最后捕获的图​​像PhotoAlbum并添加newImage。 Is it possible? 可能吗?

- (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];
}

I need to display last captured image to newImage click of alertView button: 我需要将最后捕获的图​​像显示为newImage,然后单击alertView按钮:

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

     if (buttonIndex == 1){

}
}
 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];
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM