以流格式从图像选择器上传图像到Web服务器



我需要将图像上传到流格式的web服务器。这里我使用图像选择器从图库中选择图像。

// add image data
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
[postData appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"; filename="image.jpg"rn", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: image/jpegrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[postData appendData:[[NSString stringWithFormat:@"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"--%@--rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- (IBAction)selectingPicture
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ALERT_TITLE" 
                                                        message:@"ALERT_MSG" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
#pragma mark -
   - (void) imagePickerController:(UIImagePickerController *)picker
         didFinishPickingImage:(UIImage *)image
         editingInfo:(NSDictionary *)editingInfo
{
   imageData= UIImagePNGRepresentation(image);   ////declare as a public variable in iterface
 [picker dismissModalViewControllerAnimated:YES];
}


- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}
-(void)WebserviceCallMtd
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Martin" forKey:@"names"];
[request addPostValue:@"Newyork" forKey:@"City"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];
}

下面是我的代码,我在我的项目中使用

- (void)uploadFile:(NSData*)imgData FileName:(NSString*)fileName Path:(NSString*)postPath{
isFileUploading = YES;
if (appDel.isOnline)
{
    NSLog(@"file path = %@",postPath);
    NSData *_lastImageData = [NSData dataWithData:imgData];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:postPath]];
    [request setHTTPMethod:@"POST"];
    [request addValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[_lastImageData length]] forHTTPHeaderField:@"Content-Length"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ;
    operation.inputStream =  [NSInputStream inputStreamWithData:_lastImageData];

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        if (totalBytesExpectedToWrite > 0) {
            float progress = totalBytesWritten*100/totalBytesExpectedToWrite;
            NSLog(@"%f",progress);
        }
    }];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"Sent image: ");
        [self.delegate uploadCompletedWithStatus:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failed to send image");
        NSLog(@"Error sending:  Code:%li",(long)[error code]);

    }];

    [operation start];
}
else
{
}   

}

最新更新