单击相机时应用程序终止使用图像按钮目标c

Posted

技术标签:

【中文标题】单击相机时应用程序终止使用图像按钮目标c【英文标题】:Application terminating while clicking camera use image button objective c 【发布时间】:2018-02-06 13:00:32 【问题描述】:

我的应用程序终止显示来自调试器的消息:在进行严格搜索后由于信号 9 而终止没有找到任何东西,我还检查了内存泄漏但没有找到任何东西..

问题说明 - 当我从我的应用程序打开相机并在我选择使用图像时捕获图像后,我的应用程序终止。

我的代码

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

   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
   [picker dismissViewControllerAnimated:YES completion:NULL];


【问题讨论】:

刚刚添加到 Info.plist:NSPhotoLibraryAddUsageDescription$(PRODUCT_NAME) 需要保存照片 这对我有用 :) 控制台会显示崩溃的原因。 【参考方案1】:

我曾经使用带有 uialertactionsheet 的 imagepicker 从相机和图库中选择图像。这段代码对我有用,所以你试试这个。并使用这些委托的

- (IBAction)Camera_Click:(id)sender

        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    // Photos from Gallery by calling Method
        UIAlertAction *choose = [UIAlertAction actionWithTitle:@"Select from photos" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

        [self performSelector:@selector(choosePhotoFromGallery)];

    [alertView dismissViewControllerAnimated:YES completion:nil ];

        ];

        //Take New Photo From Camara
        UIAlertAction *Capture = [UIAlertAction actionWithTitle:@"Capture from camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

     [self performSelector:@selector(takePhotoFromCamara)];

        [alertView dismissViewControllerAnimated:YES completion:nil ];

            ];
        UIAlertAction *Cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)



            ];

        //Adding the alertView Actions

        [alertView addAction:choose];
        [alertView addAction:Capture];
        [alertView addAction:Cancel];

     //Presents the AlertView
        [self presentViewController:alertView animated:YES completion:nil];


    
- (void)takePhotoFromCamara 
    

        UIImagePickerController *Take_pic = [[UIImagePickerController alloc] init];
        Take_pic.delegate = self;
            Take_pic.allowsEditing = YES;
            Take_pic.sourceType = UIImagePickerControllerSourceTypeCamera;

            [self presentViewController:Take_pic animated:YES completion:NULL];

    


- (void)choosePhotoFromGallery 
    

        UIImagePickerController *Choose_picker = [[UIImagePickerController alloc] init];
            Choose_picker.delegate = self;
            Choose_picker.allowsEditing = YES;
            Choose_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            [self presentViewController:Choose_picker animated:YES completion:NULL];

    


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

            UIImage *imgPicker = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
            self.your_imgView.image = imgPicker;

            [picker dismissViewControllerAnimated:YES completion:NULL];

    


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
    

        [picker dismissViewControllerAnimated:YES completion:NULL];

    


**Finally you add this two in your app info.plist file, otherwise app will crash.
  <key>NSPhotoLibraryUsageDescription</key>
  <string><$YourAppname$>We access your photo library.</string>
  <key>NSCameraUsageDescription</key>
  <string><$YourAppname$>We access your camera.</string>**

【讨论】:

以上是关于单击相机时应用程序终止使用图像按钮目标c的主要内容,如果未能解决你的问题,请参考以下文章

Android - 如何从相机捕获图像或从库中添加

swift核心数据:保存相机中的图像

底部带有相机按钮的 ImageView

如何使用按钮实现自定义相机?

从相机单击后在 tableView 单元中添加图像

将更改的图像保存到相机胶卷时因内存问题而终止