Converting UIImage into NSData

I need to upload a image from iOS device to web service. While uploading image i used;

    NSData* pictureData = UIImagePNGRepresentation(originalImage);

Actually it works fine. However the images i uploaded does not show up in IE. I tried uploading both JPG and PNG i can see the images in all other browsers but it does not work in IE. So i thought that it can be about converting it to NSData.

Is there any other way to convert UIImage to NSData?

Or any other idea that might be causing the problem?

Here is the way i upload the image;

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

NSMutableData *body = [NSMutableData data];

NSData* pictureData = UIImagePNGRepresentation(originalImage);

if (pictureData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.JPG\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:pictureData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

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

Upvotes: 0

Views: 2097

Answers (1)

Nicolas Thenoz
Nicolas Thenoz

Reputation: 116

You are converting your image to a PNG representation and then you rename it image.JPG

IE might get confused by that. Try to either rename your image to image.PNG or use UIImageJPEGRepresentation

Upvotes: 1

Related Questions