我想在ios中发送带有图片上传的用户ID

时间:2016-07-01 13:57:55

标签: ios objective-c

我想上传图片并在我的代码中正确上传,但我想在帖子中发送一个用户ID和图片,请有人解释。

我想上传图片并在我的代码中正确上传,但我想在帖子中发送一个用户ID和图片,请有人解释。

 - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2;
        self.profileImageView.clipsToBounds = YES;

        dic= [[NSMutableDictionary alloc]init];

        NSUserDefaults *defaults10 = [NSUserDefaults standardUserDefaults];
        NSData *imageData = [defaults10 dataForKey:@"image"];
        UIImage *contactImage = [UIImage imageWithData:imageData];
        profileImageView.image = contactImage;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        id1 = [defaults objectForKey:@"ID"];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (IBAction)Pictureupload:(id)sender
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init] ;
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentModalViewController:picker animated:YES];


    }

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
    {
       // profileImageView.image = image;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

            dispatch_async(dispatch_get_main_queue(), ^{
                NSDate *currentDate = [[NSDate alloc] init];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
                NSString *localDateString = [dateFormatter stringFromDate:currentDate];
                NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""];
                NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1];
                NSString *finalUniqueImageNAme ;
                finalUniqueImageNAme = [cleanedString2 stringByAppendingString:@".jpg"];
                NSData *imageData = UIImageJPEGRepresentation(image, 90);
                NSString *urlString = @"http://vygears.com/tcdc-portfolio/Abdul/chat/Pupload_file.php";
                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
                [request setURL:[NSURL URLWithString:urlString]];
                [request setHTTPMethod:@"POST"];
                NSString *boundary = @"---------------------------14737809831466499882746641449";
                NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
                [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

                NSMutableData *body = [NSMutableData data];
                [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n",finalUniqueImageNAme] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

                [body appendData:[NSData dataWithData:imageData]];            
                [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [request setHTTPBody:body];

                NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
                NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
                NSLog(@"Successfully uploaded");

                NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
                if(conn)
                {
                    NSLog(@"Connection Successful");
                    [self dismissModalViewControllerAnimated:true];
                }
                else
                {
                    NSLog(@"Connection could not be made");
                }
             });
        });
        [picker dismissModalViewControllerAnimated:YES];

    }


    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {

        webdata =[[NSMutableData alloc]init];

    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

        [webdata appendData:data];

    }
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

    {
        NSLog(@"%@",error);
    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

            dispatch_async(dispatch_get_main_queue(), ^{
    //               NSString *responseString = [[NSString alloc]initWithData:webdata encoding:NSUTF8StringEncoding];

                dic=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
                 NSLog( @"Success %@",dic);

           NSString *   res = [dic objectForKey:@"url"];
           NSURL *imageURL = [NSURL URLWithString:res];
           NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
            // Update the UI
            self.profileImageView.image = [UIImage imageWithData:imageData];
            NSUserDefaults *defaults10 = [NSUserDefaults standardUserDefaults];
            [defaults10 setObject:imageData forKey:@"image"];
            [defaults10 synchronize];
            });
        });    
    }

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {

        [picker dismissModalViewControllerAnimated:YES];
    }

1 个答案:

答案 0 :(得分:1)

我已经这样做了,现在工作正常。

  //  ----------------------------------Upload Image --------

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n",finalUniqueImageNAme] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//  ----------------------------------Passing user_id in Post--------

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:id1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];   
[request setHTTPBody:body];


NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Successfully uploaded");
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];