简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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