【问题标题】:Image upload in iphone [closed]iphone中的图片上传[关闭]
【发布时间】:2012-04-28 17:57:10
【问题描述】:


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


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

【问题讨论】:

  • SO 不是gimme teh codez 网站。您应该尝试提出可以回答的问题,而不必提供端到端的解决方案。你实际上是来这里学习的,而不是让别人来做你的工作,不是吗?
  • 我知道Till是对的,但他说他是新人..请不要小看别人..只要帮助他,如何解决问题
  • @Marvin 他可能是新人,但这并不妨碍他使用 Google 或 Apple 开发者网站。那里有很多例子,他甚至在他的问题中提到了 ImageViewController。那么为什么不从尝试一些东西开始呢?

标签: iphone ios xcode


【解决方案1】:

使用 UIImagePickerController 及其委托方法,并在控制器的 .h 文件中实现 UIImagePickerControllerDelegate

只需为您的按钮单击编写此代码

-(IBAction)btnTapped:(id)sender
{
        UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
        imgPicker.delegate = self;
        imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:imgPicker animated:YES];
} 

当你点击这个按钮时 UIImagePickercontroller 会加载,选择你想要的任何图片,这个 UIImagePickerController 的委托方法会被调用

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
    //This is selected image
    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
    YOURIMAGEVIEW.image = img;
}

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

现在搜索ASIHttpRequest 以了解如何将图像上传到网络服务器...尝试一些 谷歌搜索..

【讨论】:

    【解决方案2】:

    首先你必须得到图片形式的照片库,以便使用下面的代码。

                       - (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 方法将该图像发送到服务器

    这里是代码

                      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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      相关资源
      最近更新 更多