私のアプリでは、ユーザーが画像を切り取り、それを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];
これは、別のView Controllerでそれを取り戻し、それを画像に戻そうとしたときです
-(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];
}
投稿後に画像が破損するのを防ぐにはどうすればよいですか..または、画像を取り戻したら、画像をきれいにしたり、発生している破損を防ぐにはどうすればよいですか! これは私を夢中にさせています..いくつかのコードで明確な答えをいただければ幸いです.Thanks
これは、画像を送信する前に 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];