how to upload image file from document directory to php server in iphone

后端 未结 4 692
别那么骄傲
别那么骄傲 2021-01-17 04:04

I am currently working on gallery kind of iPhone application. According to my requirement, i need to store all the camera captured image into document directory. there is so

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 05:06

    You need to use/ call the API in a recursive function to get this done.

    Serverside Code

    //Create a folder named images in your server where you want to upload the image.
     // And Create a PHP file and use below code .
    
    
     
    

    Application Side code

     - (IBAction)uploadClicked:(id)sender
     {
    
     [self  UploadTheImageNow:0];
    
     }
    

    The below code is for the recursive function which will continue till all the image upload will get finish.

    -(void)UploadTheImageNow:(NSInteger)count
     {
    
     // UIImage *updatedImage=[UIImage ImageNamed:@" Your Image name / you can get from array"];
     UIImage *updatedImage=[UIImage ImageNamed:[yourArray ObjectAtIndex:ic]];
     NSData *imageData = UIImageJPEGRepresentation(updatedImage, 90);
     NSString *urlString = @"your URL link";
     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
     [request setURL:[NSURL URLWithString:urlString]];
     [request setHTTPMethod:@"POST"];
    
    
     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
     NSMutableData *body = [NSMutableData data];
     [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[NSData dataWithData:imageData]];
     [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [request setHTTPBody:body];
    
    
     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
     completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
       if ([(NSHTTPURLResponse *)response statusCode]==200)
        {
          id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
          NSLog(@"Finished with jsonObject %@", jsonObject);
          count++;
          if(count<20)
             {
               [self  UploadTheImageNow:count];
             }
            else
              {
                // Finished Uploading all Images
              }
    
        }
    
     }];
    
    
     }
    

提交回复
热议问题