简体   繁体   English

上传图片选择器照片ios

[英]Uploading image picker photo ios

I have the image picker which retrieves an image from the inbuilt camera: 我有图像选择器从内置摄像头检索图像:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [info objectForKey:UIImagePickerControllerEditedImage];
    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    self.imageView.image = chosenImage;
    NSData *image = UIImagePNGRepresentation(chosenImage);
    [self setImageDataToSend:image];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

I then want to upload the photo using http 然后,我想使用http上传照片

like this: 像这样:

 NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"image.png"]);  

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:[NSURL
                                                 URLWithString:@"http://******.co.uk/***/imageupload.php"]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"image/png"
   forHTTPHeaderField:@"Content-type"];
    [request setValue:[NSString stringWithFormat:@"%lu",
                       (unsigned long)[imageData length]]
   forHTTPHeaderField:@"Content-length"];
    [request setHTTPBody:imageData];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

This picks an image called "image.png" from the app. 这会从应用程序中选择一个名为“image.png”的图像。 I want it to pick up the NSData *image from the image picker 我希望它从图像选择器中获取NSData *图像


here is also the imageupload.php : 这里也是imageupload.php

<?php
$handle = fopen("image.png", "wb"); // write binary

fwrite($handle, $HTTP_RAW_POST_DATA);

fclose($handle);

print "Received image file.";
?>

Do you know of any better ways to do this? 你知道更好的方法吗?

You call a method setImageDataToSend: with the data you want to send and then ignore it in this line: 您使用要发送的数据调用方法setImageDataToSend:然后在此行中忽略它:

 NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"image.png"]);  

You probably want to omit that line and change: 您可能想要省略该行并进行更改:

 [request setHTTPBody:imageData];

to: 至:

 [request setHTTPBody:[self imageDataToSend]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM