【问题标题】:Upload image to web server上传图片到网络服务器
【发布时间】:2016-08-20 07:48:43
【问题描述】:

我想将UIImage 上传到网站。我已经实现了一些代码。我已经从第一页中选择了图像并在第二页上传。在此 -----[body appendData:[NSData dataWithData:_imgData ]];---- _imgData 变为 nil。在此处输入代码我已经写了一些代码,但我不知道我的错误在哪里。

    My Code is:

           -(void)ImgaeCropped:(UIImage *)image
    {
        _imageView.image=image;

        Register2 *reg2=[self.storyboard instantiateViewControllerWithIdentifier:@"Register2"];
      //  reg2.self.selected_image=_imageView.image;

        reg2.imgData=imageData;

        picture_view.hidden=YES;
    }

    - (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        @try {
           // transparent_view.hidden=YES;
            UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            [indicator startAnimating];
            indicator.frame=CGRectMake(0, 0, 10, 10);
            indicator.center = CGPointMake(CGRectGetMidX(_imageView.bounds), CGRectGetMidY(_imageView.bounds));
            [_imageView addSubview:indicator];
            [indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
            [indicator setColor:[UIColor grayColor]];
            [indicator startAnimating];

            [self dismissViewControllerAnimated:YES completion:^ {
                [indicator removeFromSuperview];
                [indicator stopAnimating];
                CropViewController *crop=[self.storyboard instantiateViewControllerWithIdentifier:@"CropViewController"];
                crop.cropDelegate=self;
                crop.image=[info objectForKey:UIImagePickerControllerOriginalImage];

    //           UIImageView *imgvUserImage = [[UIImageView alloc]init];
                UIImage *imgvUserImage= [info objectForKey:UIImagePickerControllerOriginalImage];

                imageData=UIImageJPEGRepresentation(imgvUserImage, 0.9);

                [self presentViewController:crop animated:YES completion:nil];
            }];

        }
        @catch (NSException *exception) {
            //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Finish Picking" message:exception.description delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            //        [alert show];
        }
        @finally {
            //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Finish Picking" message:@"Final" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            //        [alert show];
        }

    }


And the image selection code is ---------------

-(void)ImgaeCropped:(UIImage *)image
{
    _imageView.image=image;

    Register2 *reg2=[self.storyboard instantiateViewControllerWithIdentifier:@"Register2"];
  //  reg2.self.selected_image=_imageView.image;

    reg2.imgData=imageData;

    picture_view.hidden=YES;
}

- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    @try {
       // transparent_view.hidden=YES;
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [indicator startAnimating];
        indicator.frame=CGRectMake(0, 0, 10, 10);
        indicator.center = CGPointMake(CGRectGetMidX(_imageView.bounds), CGRectGetMidY(_imageView.bounds));
        [_imageView addSubview:indicator];
        [indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [indicator setColor:[UIColor grayColor]];
        [indicator startAnimating];

        [self dismissViewControllerAnimated:YES completion:^ {
            [indicator removeFromSuperview];
            [indicator stopAnimating];
            CropViewController *crop=[self.storyboard instantiateViewControllerWithIdentifier:@"CropViewController"];
            crop.cropDelegate=self;
            crop.image=[info objectForKey:UIImagePickerControllerOriginalImage];

//           UIImageView *imgvUserImage = [[UIImageView alloc]init];
            UIImage *imgvUserImage= [info objectForKey:UIImagePickerControllerOriginalImage];

            imageData=UIImageJPEGRepresentation(imgvUserImage, 0.9);

            [self presentViewController:crop animated:YES completion:nil];
        }];

    }
    @catch (NSException *exception) {
        //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Finish Picking" message:exception.description delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        //        [alert show];
    }
    @finally {
        //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Finish Picking" message:@"Final" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        //        [alert show];
    }

}

【问题讨论】:

  • 你有什么问题?!
  • 抱歉,我们不想猜测您的错误。投票结束。

标签: ios objective-c post image-upload


【解决方案1】:

将 AFNetworking 添加到您的项目中

https://github.com/AFNetworking/AFNetworking

 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Loading";

NSMutableDictionary* data=[NSMutableDictionary new];
//if you want to add parameters with the image
NSData *imageData = UIImageJPEGRepresentation(selectedImage, 0.5);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = responseSerializer;

AFHTTPRequestOperation *op = [manager POST:@"http://www.pickkup.com/webservice/uploadpictureios.php" parameters:data constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [hud hide:YES];
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [hud hide:YES];

    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];

[op start];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 2017-12-31
    • 2011-06-01
    相关资源
    最近更新 更多