Detect whether an UIImage is PNG or JPEG?

后端 未结 2 1022
一整个雨季
一整个雨季 2020-12-31 03:19

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 t

相关标签:
2条回答
  • 2020-12-31 04:08

    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).

    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.

    0 讨论(0)
  • 2020-12-31 04:12

    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) {
    
    }
    
    0 讨论(0)
提交回复
热议问题