3

我有一个 base64 编码的 imageview 发送到 php,当我回显它时,当我尝试将它转回 imageview 时它已损坏。我的解码器可以工作,因为当我在本地解码它时它工作正常..所以一旦我将它发送到 php,它就必须被破坏。这是日志错误消息:

   Jan  8 14:21:33 Vessel.local Topic[11039] <Error>: ImageIO: JPEG Corrupt JPEG data:214          extraneous bytes before marker 0x68  Jan  8 14:21:33 Vessel.local Topic[11039] <Error>: ImageIO: JPEG Unsupported marker type 0x68

有人可以告诉我这意味着什么,以及一旦我将字符串发送到服务器后,我如何无法防止字符串被损坏。我使用普通的 iOS 发布代理并且没有错误,唯一的错误是一旦我将其转换为 imageview。

这是将图像编码为 base64 的代码:

  NSData *imageData = UIImageJPEGRepresentation(crop, 1.0);
    NSString *baseString = [imageData base64EncodedString];
    int original =[baseString length];
    NSLog(@"orignal String=%@",baseString);
    [self register:username.text withPass:password.text withEmail:email.text withQuote:quote.text withPic:baseString];

这是我用来将图像发送到我的 php 服务器的代码:

NSString *urlString = [NSString stringWithFormat:@"http://localhost/Topic/Mobile/auth/registration.php"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
NSString *param = [NSString stringWithFormat:@"username=%@&password=%@&email=%@&quote=%@&pic=%@",user,password,email,quote,pic];
[request setHTTPBody:[param dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];

这是我用来将 base64 字符串转换回图像的代码:

   -(void)connectionDidFinishLoading:(NSURLConnection *)connection{

homeData = [NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];

NSString *decodeString = [[homeData objectAtIndex:0]objectForKey:@"profile_pic" ];
//int returnNUM = [decodeString length];
NSLog(@"RETURN STRING=%@",decodeString);
NSData *data = [NSData dataFromBase64String:decodeString];
profilepic = [[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 120, 120)];
profilepic.image = [UIImage imageWithData:data];
UIGraphicsBeginImageContextWithOptions(profilepic.bounds.size, NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:profilepic.bounds cornerRadius:80.0] addClip];
[profilepic.image drawInRect:profilepic.bounds];
profilepic.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.view addSubview:profilepic];

}

编码器和解码器工作正常......当我将字符串发布到网络时,它是如何搞砸的。我尝试将其发送到 php 后立即将其回显,但它仍然损坏..所以当它离开手机的那一刻,内容发生了变化。请帮助或请上传如何在一个帖子中一起发布图像和 NSSTRING 参数的示例

4

0 回答 0