2015-10-16 3 views
0

Вот код. Он имеет takePhoto способ для загрузки камеры. selectPhoto для 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

ответ

0

Я решил проблему, поскольку изображение приближалось как null. Со следующим кодом:

if (representation.filename!=nil) { 
      selectedFileName = [representation filename]; 
     }else{ 
      selectedFileName = @"user.jpeg"; 
     } 
Смежные вопросы