1

私のアプリでは、ユーザーが画像を切り取り、それを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=%@&quote=%@&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];
4

1 に答える 1

0

「stringByTrimmingCharactersInSet」呼び出しを行っているのはなぜですか? エンコードされた画像におそらく必要な文字を削除するように思えます...

私が使用しているコードの例を含めるように編集して、いくつかの画像を http フォームに正しく埋め込み、php ページに送信します。

NSMutableData で次のカテゴリを使用します...

以下は NSMutableData+HTTP.h の内部に入ります

@interface NSMutableData (HTTP)

-(void)appendFileAtPath:(NSString*)fullFilePath withFormKey:(NSString*)formKey withSuggestedFileName:(NSString*)suggestedFileName boundary:(NSData*)boundary;
-(void)appendKey:(NSString*)key value:(NSString*)value boundary:(NSData*)boundary;

@end

以下は NSMutableData+HTTP.m の内部に入ります

#import "NSMutableData+HTTP.h"

@implementation NSMutableData (HTTP)

//use for attaching a file to the http data to be posted
-(void)appendFileAtPath:(NSString*)fullFilePath withFormKey:(NSString*)formKey withSuggestedFileName:(NSString*)suggestedFileName boundary:(NSData*)boundary{

    [self appendData:boundary]; 

    [self appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", formKey, suggestedFileName] dataUsingEncoding:NSUTF8StringEncoding]];

    [self appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [self appendData:[NSData dataWithContentsOfFile:fullFilePath]];

    [self appendData:[@"Content-Encoding: gzip\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [self appendData:boundary]; 
}

//use for attaching a key value pair to the http data to be posted
-(void)appendKey:(NSString*)key value:(NSString*)value boundary:(NSData*)boundary{    
    [self appendData:boundary]; 
    [self appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
    [self appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
    [self appendData:boundary]; 
}

@end

次に、http フォーム送信を作成するコードで、次のようにして、いくつかの画像とキーと値のペアを (上記のカテゴリ メソッドを介して) フォームに添付します... (注: NSURLConnectionDelegate メソッドは含まれていません。フォーム送信の進行状況を追跡する場合は、実装する必要があります。)

NSString *boundaryString = @"0xKhTmLbOuNdArY";
NSData *boundaryData = [[NSString stringWithFormat:@"\r\n--%@\r\n", boundaryString] dataUsingEncoding:NSUTF8StringEncoding];
NSString *phpReceiverURL = @"http://www.someurl.com/somephppage.php";

NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setTimeoutInterval:30];
[theRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[theRequest setHTTPShouldHandleCookies:NO];
[theRequest setURL:[NSURL URLWithString:phpReceiverURL]];
[theRequest setHTTPMethod:@"POST"]; 

[theRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundaryString] forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];

//add some image files to the form
[body appendFileAtPath:[imagePath stringByAppendingPathComponent:@"cat.jpg"] 
    withFormKey:@"cat"  
    withSuggestedFileName:@"received-cat.jpg"
    boundary:boundaryData
];

[body appendFileAtPath:[imagePath stringByAppendingPathComponent:@"dog.jpg"] 
    withFormKey:@"dog"  
    withSuggestedFileName:@"received-dog.jpg"
    boundary:boundaryData
];

//add some key value pairs to the form
[body appendKey:@"testkeyone" value:@"testvalueone" boundary:boundaryData]; 
[body appendKey:@"testkeytwo" value:@"testvaluetwo" boundary:boundaryData]; 

// setting the body of the post to the reqeust
[theRequest setHTTPBody:body];

//create the connection
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

また、アプリの外でこれをテストしていないため、何かを忘れている可能性があります (上記の例で定義していない「imagePath」など - 画像のパスを指定する必要があります)。 PHPページに送信されるフォームにいくつかの画像を添付する方法の良いアイデアを提供するはずです.

お役に立てれば!

于 2013-01-08T22:03:44.863 に答える