Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 尝试从照相机捕获图像时出错_Ios_Ios8_Camera_Uiimagepickercontroller - Fatal编程技术网

Ios 尝试从照相机捕获图像时出错

Ios 尝试从照相机捕获图像时出错,ios,ios8,camera,uiimagepickercontroller,Ios,Ios8,Camera,Uiimagepickercontroller,这是代码。它有拍照方法来装载相机为CameraRoll选择照片 当我试图从相机卷中选择照片时,它运行良好,我可以上传图像 当我试图从相机拍照并上传时,应用程序崩溃了 - (void)takePhoto { if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIAlertView *deviceNotFoundAlert = [

这是代码。它有拍照方法来装载相机为CameraRoll选择照片

当我试图从相机卷中选择照片时,它运行良好,我可以上传图像

当我试图从相机拍照并上传时,应用程序崩溃了

- (void)takePhoto {

    if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:@"No Device" message:@"Camera is not available"
                                                                     delegate:nil
                                                            cancelButtonTitle:@"Ok"
                                                            otherButtonTitles:nil];
        [deviceNotFoundAlert show];

    }else{

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

        [self presentViewController:picker animated:YES completion:NULL];
    }
}
- (void)selectPhoto {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    //picker.modalPresentationStyle = UIModalPresentationCurrentContext;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

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


}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    chosenImage = info[UIImagePickerControllerEditedImage];
    self.imgUser.image = chosenImage;
    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *representation = [myasset defaultRepresentation];
        selectedFileName = [representation filename];
        NSLog(@"fileName : %@",selectedFileName);

        encodedImage = [UIImageJPEGRepresentation(chosenImage, 0.8) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        //NSLog(@"Encoded Image : %@", encodedImage);

        NSString *newImage;
        NSString *newImage1;

        newImage = [encodedImage stringByReplacingOccurrencesOfString:@"+"
                                                           withString:@"%2B"];
        newImage = [newImage stringByReplacingOccurrencesOfString:@"/"
                                                           withString:@"%2F"];
        newImage1 = [newImage stringByReplacingOccurrencesOfString:@"="
                                                        withString:@"%3D"];
        //NSLog(@"Encoded Image : %@", newImage1);

        NSLog(@"Selected File Name : %@", selectedFileName);
        //Sending Upload Request
        [activityIndicator startAnimating];
        [[MyService MySingleton] sendRequestWithHeader:_headerString param:[NSArray arrayWithObjects:newImage1,selectedFileName, nil] msg:@"uploadPhoto"];
    };

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:imageURL
                   resultBlock:resultblock
                  failureBlock:nil];




    [picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}
得到以下东西:

对尚未渲染的视图进行快照将导致空快照。确保您的视图在快照之前或屏幕更新之后至少呈现一次


在拍摄照片并尝试使用didFinishPickingMediaWithInfo方法访问图像时,也将图像名称设置为null。

我已经解决了图像设置为null的问题。使用以下代码:

if (representation.filename!=nil) {
            selectedFileName = [representation filename];
        }else{
            selectedFileName = @"user.jpeg";
        }

你设置了代理吗?是的,他们已经设置好了。错误是什么?它在哪里崩溃?一定是在你的资产库块中发生了什么。使用断点来调试它