Search code examples
iosuiimagepickercontrollersendbird

SendBird - iOS: Cannot receive camera photo, but can receive stored image


I'm adding a send picture feature to my app using SendBird.

On - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info I execute this code

NSData *imageFileData = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage], 0.5);
NSURL* imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
NSString* imageName = [imagePath lastPathComponent];

[self.currentChannel sendFileMessageWithBinaryData:imageFileData filename:imageName type:@"image/jpg" size:[imageFileData length] data:nil completionHandler:^(SBDFileMessage * _Nullable fileMessage, SBDError * _Nullable error) {}];

This code works well when the imagePicker's sourcetype is: UIImagePickerControllerSourceTypePhotoLibrary.

If I send an image with imagePicker which's sourcetype is: UIImagePickerControllerSourceTypeCamera then

- (void)channel:(SBDBaseChannel *)sender didReceiveMessage:(SBDBaseMessage *)message is never triggered.


Solution

  • As I know, the info dictionary from UIImagePickerControllerSourceTypeCamera doesn't have an object for UIImagePickerControllerReferenceURL. So, the imagePath and the imageName must be nil.

    The sendFileMessageWithBinaryData:filename:type:size:data:completionHandler: needs filename that is not nil. Because you generate jpeg data, just use @"image.jpg" as the filename like this:

    NSData *imageFileData = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage], 0.5);
    
    [self.currentChannel sendFileMessageWithBinaryData:imageFileData filename:@"image.jpg" type:@"image/jpg" size:[imageFileData length] data:nil completionHandler:^(SBDFileMessage * _Nullable fileMessage, SBDError * _Nullable error) {
    }];