【问题标题】:How to load captured image from photo album如何从相册加载捕获的图像
【发布时间】:2014-04-08 08:12:10
【问题描述】:

我有一个按钮来捕捉图片,然后保存到 photoAlbum。我使用另一个按钮从使用imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 加载图像,然后我实现didFinishPickingMediaWithInfo: 将相册加载到newImage。我需要的是在拍摄照片后我有 alertview 然后单击是按钮自动加载最后拍摄的图像 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){

}
}

【问题讨论】:

    标签: ios uiimage uiimagepickercontroller photo-gallery


    【解决方案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];
            }
        }
    

    【讨论】:

    • 此处未声明的变量选择器:[picker dismissViewControllerAnimated:YES completion:nil];
    • 在 .h 文件中声明 UIImagePickerController *myPicker。
    • 我做到了。但它不是从照片库中获取图像
    • 我的问题是我需要从照片库加载图片而不选择图片
    • 您是否使用 UIImagePickerController 捕获图像?
    猜你喜欢
    • 1970-01-01
    • 2012-11-08
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 2010-12-01
    • 2021-07-16
    相关资源
    最近更新 更多