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

时间:2015-10-16 13:31:49

标签: ios ios8 camera uiimagepickercontroller

这是代码。它有 takePhoto 方法来加载相机。 CameraRoll的 selectPhoto

当我尝试从相机胶卷中选择照片时效果很好,我可以上传图像。

当我尝试从相机拍摄照片并上传而不是应用程序崩溃。

- (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。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,因为图像是空的。使用以下代码:

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