我正在开发聊天,在聊天中我需要分享图片,我的应用程序有一个按钮从图库中选择图像。
这是我的按钮代码:
[upBtn2 addTarget:self action:@selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];
在图库中,用户可以选择要与其他人聊天时共享的图片,对话只是1对1聊天。
这是我的代码。
- (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];
}
}
我遇到了问题,它显示的代码
2014-10-14 11:01:21.973 Ucity [2907:60b]消息:( { 文本=" / 9J / 4AAQSkZJRgABAQAAAQABAAD / 4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAE9KADAAQAAAABAAAFcAAAAAD / 2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH /... 34;
我需要在那里看到图像,但由于某种原因它显示了文字。
我正在使用GTMBase 64库
#import "GTMBase64.h"
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
那个神秘的文字是图像。它只是图像的base-64字符串表示。我假设您正在进行这种base-64编码的原因(例如,您要上传图像的某些服务需要此格式)。
我从你的问题中假设你想在某个地方展示图像。我只是想用这个代码来协调它,你在其中检索图像,将其转换为base-64字符串,然后丢弃原始图像,现在你问我们为什么你只看到字符串。
如果您再次需要图片,可以选择以下几种方法:
保留您在UIImage
中抓取的NSData
(或didFinishPickingMediaWithInfo
)。
您也可以将base-64字符串转换回NSData
(然后从中创建UIImage
)。这是一个非常复杂的方法,但它会起作用。
顺便说一句,如果您愿意,可以使用Apple在iOS 7中公开的本机base-64方法替换GTMBase64.h
。请参阅https://stackoverflow.com/a/19794564/1271826。
此外,我个人并不喜欢抓取UIImage
并进行PNG转换以获取NSData
。我总是把原始资产当作shown here。这样可以确保信息不会丢失,并且生成的NSData
不会超出需要的范围。