将图像选择器中的图像以流格式上传到Web服务器

时间:2014-06-07 09:39:41

标签: ios objective-c ios7 nsstream

我需要以流格式将图像上传到网络服务器。在这里,我使用Image Picker从图库中选择图像。

3 个答案:

答案 0 :(得分:0)

- (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"];
}

答案 1 :(得分:0)

// 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:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:imageData];
[postData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

答案 2 :(得分:0)

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

- (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
{

}   

}