在iphone中上传图片 [英] Image upload in iphone

查看:35
本文介绍了在iphone中上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是 iPhone 开发的新手.我想将图像从 Iphone 库上传到服务器.我想在屏幕上点击一个按钮,通过点击按钮用户可以看到图像文件并选择用户想要的一个文件.选择文件后,我想在图像视图中显示图像.
怎么做.如何使用 ImageViewController?


I am new in Iphone development. I want to upload image from Iphone library to a server. I want to take a button on screen, by clecking button user can see the image files and select one file which user want. After selecting file I want to show the image in image view.
How to do it. How to use ImageViewController?


谢谢,
阿维纳什·帕蒂尔.


Thanks,
Avinash Patil.

推荐答案

首先,您必须在代码下方获得用于该用途的图像表单照片库.

first u have to get image form photo library for that use below code.

                   - (IBAction)BrowseImage:(id)sender {

if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = 
    UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              nil];
    imagePicker.allowsEditing = NO;
    [self presentModalViewController:imagePicker animated:YES];
    //newMedia = NO;

  }

  }
         -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
         {
       [self dismissModalViewControllerAnimated:YES];

         }

选择图像后,您使用http post方法将该图像发送到服务器

after selecting image u have send that image to server using http post method

这是代码

                  NSString *urlString = @"your Url";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSMutableData *body = [NSMutableData data];


NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

// file
NSData *imageData = UIImageJPEGRepresentation([self scaleAndRotateImage:Mainimage.image],90);


[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"user_photo\"; filename=\"photoes.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

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

[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];




// Text parameter1
// NSString *param1 = @"parameter text";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:ImageTitel.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];

这篇关于在iphone中上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆