在iOS中通过POST上传NSData图像

时间:2014-11-26 16:11:59

标签: ios objective-c post

我有一个包含一些人物信息和图像的表格。我尝试使用此功能在帖子中保存信息,并且效果很好:

    - (void)Inscription:(NSArray *)value completion:(void (^)( NSString * retour))block{


        NSOperationQueue *queue = [[NSOperationQueue alloc] init];   
        NSArray *Param_header = @[@"username", @"password", @"email",@"first_name", @"last_name",@"image"];    
       // NSArray *Param_value = @[@"ali", @"aliiiiiiii", @"ali.ali@gmail.com",@"ali",@"zzzzz"];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:   [NSString stringWithFormat:@"http://  /Messenger/services/messenger_register"]]];     
        NSString *aa=[self buildParameterWithPostType:@"User" andParameterHeaders:Param_header ansParameterValues:value];    
        [request setHTTPBody:[aa dataUsingEncoding:NSUTF8StringEncoding]];      
        [request setHTTPMethod:@"POST"];      
        [NSURLConnection sendAsynchronousRequest: request
                                           queue: queue
                               completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
                                   if (error || !data) {
                                       // Handle the error

                                       NSLog(@"Server Error : %@", error);
                                   } else {

                                       NSError *error = Nil;      
                                       id jsonObjects = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];       
                                       block([NSString stringWithFormat:@"%@", [jsonObjects objectForKey:@"message"]]);        

                                   }   
                               }     
         ];        

    }       

不,我想发送照片,我首先将其转换为NSData:

[picker dismissViewControllerAnimated:YES completion:nil];

    UIImage *imag= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    imag = [imag scaleAndRotateImage:imag];


    CGFloat compression = 0.9f;
    CGFloat maxCompression = 0.1f;
    int maxFileSize = 25*1024;//was 250x1024

    NSData *imageData = UIImageJPEGRepresentation(imag, compression);

    while ([imageData length] > maxFileSize && compression > maxCompression)
    {
        compression -= 0.1;
        imageData = UIImageJPEGRepresentation(imag, compression);
    }

   NSData _D_ImageData = imageData;

我试图将其发送到服务器,但图片c上传:

 [web Inscription:Param_value completion:^(NSString *retour) {
        CustomPrpgress.hidden=true;
        NSLog(@" eee %@ ",retour);
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:retour message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];

    }];

有人可以帮助我,我是初学者,这是我的第一次。谢谢

1 个答案:

答案 0 :(得分:0)

我的代码工作正常,我在php文件中犯了错误