简体   繁体   English

尝试从相机捕获图像时出现错误

[英]Getting Error When Trying To Capture Image From Camera

Here is the code. 这是代码。 It has takePhoto method for loading camera. 它具有用于加载相机的takePhoto方法。 selectPhoto for CameraRoll. 选择“用于CameraRoll的照片”。

When I am trying to select photo from camera roll it works well and i can upload the image. 当我尝试从相机胶卷中选择照片时,效果很好,可以上传图像。

While I am trying to take photo from camera and upload than the app crashes. 当我尝试从相机拍摄照片并上传时,应用程序崩溃了。

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

}

Getting Following thing : 得到以下东西:

Snapshotting a view that has not been rendered results in an empty snapshot. 快照未渲染的视图将导致快照为空。 Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates. 确保在快照之前或屏幕更新后快照至少已渲染一次视图。

Also getting image name as null when taking a picture and trying to access it in didFinishPickingMediaWithInfo method. 在拍照并尝试通过didFinishPickingMediaWithInfo方法访问图像时, 还将图像名称获取为null。

I have solved the issue as image was coming as null. 我已经解决了这个问题,因为图片即将变为null。 With the following code : 使用以下代码:

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

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

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