检测UIImage是PNG还是JPEG?

时间:2023-01-24 21:22:43

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

目前,我通过MonoTouch完成了iPhone开发的第一步,我正在使用从照片库中读取的UIImage。

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

我要实现的是获得图像的原始字节数组(byte[])。

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

我知道有UIImageJPEGRepresentation和UIImagePNGRepresentation的MonoTouch包装。我也知道如何使用它们。我不知道的是:

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.

例如,如果原始图像是JPEG图像,我不希望将它作为PNG,而是作为JPEG,反之亦然。

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

有办法做这个吗,还是我漏掉了一些点?

2 个解决方案

#1


27  

Once you have a 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).

一旦有了UIImage,它就可以使用UIImageJPEGRepresentation或UIImagePNGRepresentation生成JPEG或PNG。只有在从它创建UIImage时,原始图像的格式才是重要的(决定由该委员会提供程序加载它)。

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.

你打算通过UIImageView来改变图像,还是图像在你的体验中保持不变?如果它没有改变,你只需要UI来选择图像,你能得到文件字节吗?例如,如果你展示图片只是为了选择然后你把它们上载到服务器或别的什么,UIImage只能用来查看和选择如果你的数据结构记得它来自哪个文件,你可以把这些数据从磁盘上拿回来并上传。如果您在视图中更改了文件,那么您或用户需要决定图像的输出(如果jpeg是质量)。

#2


1  

PREPARE

准备

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

@property (assign, nonatomic) DownloadImageType imageType;

DETECT

检测

- (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");
    }

}

USE

使用

if (self.imageType == DownloadImageTypePng) {

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

}

#1


27  

Once you have a 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).

一旦有了UIImage,它就可以使用UIImageJPEGRepresentation或UIImagePNGRepresentation生成JPEG或PNG。只有在从它创建UIImage时,原始图像的格式才是重要的(决定由该委员会提供程序加载它)。

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.

你打算通过UIImageView来改变图像,还是图像在你的体验中保持不变?如果它没有改变,你只需要UI来选择图像,你能得到文件字节吗?例如,如果你展示图片只是为了选择然后你把它们上载到服务器或别的什么,UIImage只能用来查看和选择如果你的数据结构记得它来自哪个文件,你可以把这些数据从磁盘上拿回来并上传。如果您在视图中更改了文件,那么您或用户需要决定图像的输出(如果jpeg是质量)。

#2


1  

PREPARE

准备

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

@property (assign, nonatomic) DownloadImageType imageType;

DETECT

检测

- (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");
    }

}

USE

使用

if (self.imageType == DownloadImageTypePng) {

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

}