0

デバイスからサーバーに画像をアップロードしたい。しかし、アップロードしていません!私のリターン成功値でさえnullを示しています。成功値は 1 または 0 のいずれかである必要があります。私のコードはここに示されています。私のコードに誤りがあるかどうか教えてください。助けてくれてありがとう。

-(void)ImageUpload{

NSString *urlString = [NSString stringWithFormat:@"%@upload.php", APIheader];

NSString *postLength = [NSString stringWithFormat:@"%d", [imgDATA length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:urlString]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imgDATA];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"return string: %@",returnString);

    returnString = [returnString stringByReplacingOccurrencesOfString:@"(" withString:@"["];
    returnString = [returnString stringByReplacingOccurrencesOfString:@")" withString:@"]"];
    returnString = [returnString stringByReplacingOccurrencesOfString:@";" withString:@""];
    SBJsonParser *parser = [[SBJsonParser alloc]init];
    NSArray *array = (NSArray *)[parser objectWithString:returnString error:nil];
    NSString *status = [NSString stringWithFormat:@"%@",[[array objectAtIndex:0]objectForKey:@"success"]];

    if ([status isEqualToString:@"1"]) {

        NSLog(@"Image Updated");
    }
    else{

        NSLog(@"status is: %@",status);
    }
}

[request release];

}
4

4 に答える 4

2

返して保存するCoreGraphics' メソッドを使用できます。再度変換する場合は、メソッドを使用して作成します。iPhoneのサーバーへのこの投稿画像とiOSで画像をバイナリ形式に変換する方法に関連するいくつかの質問? 同じ答えを3回与えるように。UIImagePNGRepresentation(UIImage *image)NSDataUIImage[UIimage imageWithData:(NSData *data)]

- (void)sendImageToServer {
       UIImage *yourImage= [UIImage imageNamed:@"image.png"];
       NSData *imageData = UIImagePNGRepresentation(yourImage);
       NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

       // Init the URLRequest
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
       [request setHTTPMethod:@"POST"];
       [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
       [request setHTTPBody:imageData];

       NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
       if (connection) {
          // response data of the request
       }
       [request release];
 }
于 2013-05-13T10:51:10.950 に答える
0

このコードを使用して、任意の画像をサーバーにアップロードします。これは私にとってはうまくいきます。:)

   UIImage *yourImage= [UIImage imageNamed:@"image.png"];
   NSData *imageData = UIImagePNGRepresentation(yourImage);
   NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

   // Init the URLRequest
   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
   [request setHTTPMethod:@"POST"];
   [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
   [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
   [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
   [request setHTTPBody:imageData];

   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   if (connection) {
      // response data of the request
   }
于 2013-05-13T11:09:47.903 に答える
0

これを試して

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];                                    
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

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

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
[request setURL:requestURL];

ノート

「BoundaryConstant」は基本的に変数「境界」であり、これは基本的にランダム (NSString *) であり、「FileParamConstant」は基本的に「filename.jpg」です。

于 2013-05-13T11:49:38.843 に答える