【发布时间】:2013-01-08 20:40:27
【问题描述】:
在我的应用程序中,用户裁剪那里的图片我将其编码为 base64 字符串并将其发送到我的 php 脚本。我正在使用 NSData+base64 类但是,当应用程序再次检索字符串以转换回图片我收到此错误:
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
这是我发送数据的代码:
NSString *urlString = [NSString stringWithFormat:@"http://myurl.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=%@"e=%@&pic=%@",user,password,email,quote,pic];
[request setHTTPBody:[param dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
这是我在另一个视图控制器中取回它并尝试将其转回图像的时候
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
homeData = [NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];
NSString *decodeString = [[homeData objectAtIndex:0]objectForKey:@"profile_pic" ];
decodeString = [decodeString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
int returnNUM = [decodeString length];
NSLog(@"RETURN STRING=%d",returnNUM);
NSData *data = [[NSData alloc] initWithData:[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];
[pic drawInRect:profilepic.bounds];
profilepic.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.view addSubview:profilepic];
}
一旦我发布它,我如何防止图像被损坏..或者一旦我把它拿回来,我该如何清理它或防止发生损坏!这让我发疯了。非常感谢您提供一些代码的明确答案。谢谢
这是我在发送之前将图像转换为 base64 的代码。
NSData *imageData = UIImageJPEGRepresentation(crop, 1.0);
NSString *baseString = [imageData base64EncodedString];
int original =[baseString length];
NSLog(@"orignal String=%d",original);
[self register:username.text withPass:password.text withEmail:email.text withQuote:quote.text withPic:baseString];
【问题讨论】:
-
在 PHP 方面,做一个
var_dump(substr($pic_data, 0, 512))并发布结果。可能会给我们更多的工作机会。 -
您确定图片在任一方向传输时都没有损坏吗?
-
@Marc B - 这就是我的想法..我将图像发送到服务器并立即将其回显到模拟器并且它已损坏..因此它在 php 端或传输过程中被损坏。 .我在xcode中记录了它并计算了字符并在我发送它之前进行了比较,它是来的字符数
-
因为它是base64,只需在php中解码并将其保存到某个地方的普通文件中。如果是传输故障,您会在图片中间的某个地方出现垃圾。客户端/服务器端错误可能会在图片的开头或结尾添加损坏
-
好吧,我试一试..因为我在本地执行了编码/解码以检查它是否正常工作,并且它是..所以它必须在 php 端或在传输中..我唯一的问题是..我使用的 base64 编码类是否与 php 中内置的 base64 类兼容?我在 iPhone 上使用了很多人使用的 NSDATA+BASE64 类,其次如果我在 php 中解码它会变成什么?随机字符串?一个图像?我可以将它存储为什么?
标签: php iphone ios uiimageview base64