¿Detectar si un UIImage es PNG o JPEG?

Currently doing my first steps with iPhone development through MonoTouch, I am playing with an UIImage that I read from the photo library.

What I want to achieve is to get the raw byte array (byte[]) of the image.

I know that there are the UIImageJPEGRepresentation y UIImagePNGRepresentation wrappers in MonoTouch. I also know how to use them. What I don't know is:

How do I decide which of these two functions to call?

I.e. if the original image is a JPEG image, I do not want to get it as an PNG but also as a JPEG, and vice versa.

Is there a way to do this, or am I missing some points on this?

preguntado el 27 de agosto de 11 a las 19:08

2 Respuestas

Una vez que tengas una UIImage, it is capable of producing either a JPEG or PNG using UIImageJPEGRepresentation or UIImagePNGRepresentation. The format of the original image is only important when the UIImage is being created from it (decides which CFImage provider to load it).

If it's important to your app or algorithm to ensure you save it as the original format, I think you have to maintain that info to use when your writing it. I double checked and couldn't find anything that advertised what format it came from.

Are you going to change the image through UIImageView or does the image stay unchanged in your experience? If it's not changed and you just need the UI to select the image, could you get to file bytes? For example, if you showed the images just to select and then you upload them to a server or something, the UIImage could only be for viewing and selecting and if your data structure remembers which file it came from, you could get the bits back off disk and upload. If your changing the file in the view, then you or the user needs to decide the output (and if jpeg the quality) of the image.

contestado el 17 de mayo de 14 a las 11:05

Thank you, @bryanmac Basically I am writing a small tool to select an image from the photo library and upload it through a web service. So I am not producing the image, but just "consuming" it. Maybe I could get the raw bytes somewhere in the FinishedPickingImage override's NSDictionary editingInfo ¿parámetro? - Uwe Keim

So, if i have understand, it really doesn't matters if the photo was originally uploaded as PNG or JPEG... once you get the UIImage you can convert it to both formats? - Fustigador

If there's an alpha channel, then you're going to want to know which to use. I took a jpeg and then used uiimage to display it and then saved it to a local data base, I retrospect I should have just saved it to the disk, any who I used UIImagePNGRepresentation instead of UIImageJPEGRepresentation and later tried to restore it and this is the result imgur.com/2yG3Zv2 As you can see the transparency gets messed up. As I said I should probably just saved it to the hdd and stored the path but the point is that in some cases it does matter which function you use. - Biclops

Si está usando un UIImagePickerControllermira el UIImagePickerControllerMediaType editing information key. Then, see UTI Image Content Types. - ma11hew28

PREPARAR

typedef NS_ENUM(NSInteger, DownloadImageType) {
    DownloadImageTypePng,
    DownloadImageTypeJpg
};

@property (assign, nonatomic) DownloadImageType imageType;

DETECTAR

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

    NSString *compareString = [[info objectForKey:UIImagePickerControllerReferenceURL] absoluteString];
    NSRange pngRange = [compareString rangeOfString:@"PNG" options:NSBackwardsSearch];
    if (pngRange.location != NSNotFound) {
        compareString = [compareString substringFromIndex:pngRange.location];
        self.imageType = DownloadImageTypePng;
        NSLog(@"%@", compareString);
    } else {
        NSLog(@"Not PNG");
    }

    NSRange jpgRange = [compareString rangeOfString:@"JPG" options:NSBackwardsSearch];
    if (jpgRange.location != NSNotFound) {
        compareString = [compareString substringFromIndex:jpgRange.location];
        self.imageType = DownloadImageTypeJpg;
        NSLog(@"%@", compareString);
    } else {
        NSLog(@"Not JPG");
    }

}

USO

if (self.imageType == DownloadImageTypePng) {

} else if (self.imageType == DownloadImageTypeJpg) {

}

Respondido el 15 de junio de 16 a las 14:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.