xcode 如何知道这张图片是 jpg 还是 png iphone

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10239330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:01:49  来源:igfitidea点击:

how to know this image is jpg or png iphone

iphoneiosxcodeuiimagejpegrepresentationuiimagepngrepresentation

提问by Desmond

i will like to pick an images from UIImagepicker, there are PNG and JPG format in camera roll.

我想从 UIImagepicker 中选择图像,相机胶卷中有 PNG 和 JPG 格式。

and i need to convert it into NSData. However i need to know if this images is UIImageJPEGRepresentationor UIImagePNGRepresentationso i could convert it.

我需要将其转换为 NSData。不过,我需要知道这是图像UIImageJPEGRepresentation还是UIImagePNGRepresentation,所以我可以将其转换。

UIImage *orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];    
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSData *orgData = UIImagePNGRepresentation(orginalImage);

回答by Perception

You shouldn't know or care what the internal representation of images in the camera roll is. The methods you mentioned UIImageJPEGRepresentationand UIImagePNGRepresentationreturn you representationsof the camera roll image. It's up to you to pick which representation you want to use.

您不应该知道或关心相机胶卷中图像的内部表示是什么。你提到的方法UIImageJPEGRepresentationUIImagePNGRepresentation返回你申述相机胶卷图像的。您可以选择要使用的表示形式。

To summarize:

总结一下:

NSData * pngData = UIImagePNGRepresentation(originalImage);

Will return you the image representation in an NSDataobject, with the format PNG.

将返回NSData对象中的图像表示,格式为 PNG。

回答by Ayush Goel

When the delegate method imagePickerController:didFinishPickingMediaWithInfo: for UIImagePickerController is called, you get the asset URL for the particular photo picked.

当 UIImagePickerController 的委托方法 imagePickerController:didFinishPickingMediaWithInfo: 被调用时,您将获得所选照片的​​资源 URL。

[info valueForKey:UIImagePickerControllerReferenceURL]

Now this URL can be used to access the asset in the ALAssetsLibrary. Then you would need a ALAssetRepresentation of that accessed asset. From this ALAssetRepresentation we can get the UTI for that image (http://developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html)

现在此 URL 可用于访问 ALAssetsLibrary 中的资产。然后,您将需要该访问资产的 ALAssetRepresentation。从这个 ALAssetRepresentation 我们可以得到该图像的 UTI(http://developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html

Maybe the code would make it a bit clearer :

也许代码会让它更清楚一点:

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

As the UTI explains, this should be a sure shot answer to how the image is stored in the photo library.

正如 UTI 解释的那样,这应该是对图像如何存储在照片库中的肯定回答。

回答by user1019042

in Swift 2.2

在 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 {

        }