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

问题描述:

以下是代码。它有takePhoto装载相机的方法。 selectPhoto for 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]; 

} 

获得如下的东西:

快照,在一个空的快照尚未呈现结果的视图。确保您的视图在屏幕更新后的快照或快照之前已呈现至少一次。

同时在拍照时试图访问它的图像名称为null didFinishPickingMediaWithInfo方法。

+0

您是否设置了代表? –

+0

是的,他们已经被设置。 –

+0

什么是错误?它在哪里崩溃? – rmaddy

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

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