문제

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

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. I want it to pick up the NSData *image from the image picker


here is also the 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:

 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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top