0

I am developing a chat, in chat I need to share pictures, my app has a button to select an image from gallery.

This is my button code:

[upBtn2 addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];

In gallery the user can select an image to share in chat with someone else, the conversation is just 1 to 1 chat.

This is my code.

- (IBAction)uploadImage:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                (NSString *) kUTTypeImage,
                                nil];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
        newMedia = NO;
    }
}

-(void)imagePickerControllerDidCancel:
(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];

    [self dismissViewControllerAnimated:YES completion:nil];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info
                          objectForKey:UIImagePickerControllerOriginalImage];

        Uploadedimage.image=image;

        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,
                                            @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }
    UIImage *image = [info
                      objectForKey:UIImagePickerControllerOriginalImage];
    // [self performSelector:@selector(uploadToServer) withObject:nil afterDelay:0.0];
    XMPPMessage *message = [[XMPPMessage alloc] initWithType:XMPP_MESSAGE_TYPE_CHAT to:[self.contact jid]];

    NSData *imageData = UIImagePNGRepresentation(image);
    NSString *imageStr = [GTMBase64 stringByEncodingData:imageData];

    //decoding
    NSData *imageData2 = [GTMBase64 decodeString:imageStr];

    [message addBody:imageStr];

    [[[AppDelegate appDelegate] xmppStream] sendElement:message];

    XMPPJID *myJID = [[[AppDelegate appDelegate] xmppStream] myJID];
    [self addMessage:@{
                       XMPP_TIMESTAMP: [NSDate date],
                       XMPP_MESSAGE_USERNAME: [myJID bare],
                       XMPP_MESSAGE_TEXT: imageStr,
                       }];

    [self.tableView reloadData];
    [self scrollToBottom:true];
}


- (void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
  contextInfo:(void *)contextInfo
{
    if (error) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Archivo guardado"
                              message: @"Error al guardar archivo"\
                              delegate: nil
                              cancelButtonTitle:@"Aceptar"
                              otherButtonTitles:nil];
        [alert show];
    }
}

I am getting an issue, the code it is showing

2014-10-14 11:01:21.973 Ucity[2907:60b] messages: ( { text = "/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAE9KADAAQAAAABAAAFcAAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/..."

I need to see the image there but for some reason its showing that text.

I am using GTMBase 64 library

#import "GTMBase64.h"

Can someone help me to fix this problem?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I am trying this >> when a user select one image the image should be appear in the chat and not that text, so I do not how to do this i can not find any example so I am improving – Eilean Laborde Oct 14 '14 at 17:54

1 Answers1

1

That cryptic text is the image. It's just the base-64 string representation of the image. I assume you're doing this base-64 encoding for a reason (e.g. this format is required by some service to which you are uploading the image).

I'm presuming from your question that you want to show the image somewhere. I'm just trying to reconcile this with this code, in which you retrieved the image, converted it to a base-64 string, then discarded the original image, and now you're asking us why you're only seeing the string.

If you need the image again, there are a couple of options:

  1. Keep the UIImage (or the NSData) that you grabbed in didFinishPickingMediaWithInfo.

  2. You could alternatively convert the base-64 string back to a NSData (and then create a UIImage from that). This is a pretty convoluted approach, but it would work.


As an aside, if you wanted, you could probably replace GTMBase64.h with the native base-64 methods that Apple exposed in iOS 7. See https://stackoverflow.com/a/19794564/1271826.

Also, I don't personally like grabbing the UIImage and doing the PNG conversion to get the NSData. I always grab the original asset as shown here. This ensures there's no loss of information and that the resulting NSData isn't larger than it needs to be.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044