How to recognize this image - jpg or png iphone

I like to choose images from UIImagepicker, there is PNG and JPG format in the video clip.

and I need to convert it to NSData. However, I need to know if these images are UIImageJPEGRepresentationor UIImagePNGRepresentation, therefore, I could convert it.

UIImage *orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];    
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSData *orgData = UIImagePNGRepresentation(orginalImage);
+5
source share
3 answers

You do not need to know or care about what the internal representation of the images is in the camera roll. The methods you mentioned UIImageJPEGRepresentationand UIImagePNGRepresentationreturn the images of the camera roll image. It is up to you to decide which view you want to use.

Summarizing:

NSData * pngData = UIImagePNGRepresentation(originalImage);

NSData PNG.

+9

imagePickerController: didFinishPickingMediaWithInfo: UIImagePickerController URL- .

[info valueForKey:UIImagePickerControllerReferenceURL]

URL- ALAssetsLibrary. ALAssetRepresentation . ALAssetRepresentation UTI (http://developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html)

, :

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  if (!(picker.sourceType == UIImagePickerControllerSourceTypeCamera)) {
    NSLog(@"User picked image from photo library");
    ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
    [library assetForURL:[info valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
      ALAssetRepresentation *repr = [asset defaultRepresentation];
      if ([[repr UTI] isEqualToString:@"public.png"]) {
        NSLog(@"This image is a PNG image in Photo Library");
      } else if ([[repr UTI] isEqualToString:@"public.jpeg"]) {
        NSLog(@"This image is a JPEG image in Photo Library");
      }
    } failureBlock:^(NSError *error) {
      NSLog(@"Error getting asset! %@", error);
    }];
  }
}

UTI, , .

+4

Swift 2.2

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if (!(picker.sourceType == UIImagePickerControllerSourceType.Camera)) {
        let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL
        if assetPath.absoluteString.hasSuffix("JPG") {

        } else {

        }
+1

All Articles