2015-10-16 76 views
0

以下是代碼。它有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

回答

0

我已經解決了這個問題,因爲圖像是空的。使用以下代碼:

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