【问题标题】:how to know this image is jpg or png iphoneiphone怎么知道这张图片是jpg还是png
【发布时间】:2012-05-01 15:09:51
【问题描述】:

我想从 UIImagepicker 中挑选一张图片,相机胶卷中有 PNG 和 JPG 格式。

我需要将其转换为 NSData。但是我需要知道这张图片是UIImageJPEGRepresentation 还是UIImagePNGRepresentation,以便我可以转换它。

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

【问题讨论】:

    标签: iphone ios xcode uiimagejpegrepresentation uiimagepngrepresentation


    【解决方案1】:

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

    总结一下:

    NSData * pngData = UIImagePNGRepresentation(originalImage);
    

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

    【讨论】:

    • 有时你必须小心,因为如果你从 jpeg 图像制作 UIImageJPEGRepresentation,你可能会不必要地压缩已经压缩的图像(损失质量和处理时间)。例如,您可能需要仅接受 jpeg 的库进一步处理拾取的图像。
    【解决方案2】:

    当调用 UIImagePickerController 的委托方法 imagePickerController:didFinishPickingMediaWithInfo: 时,您将获得所选照片的​​资产 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 解释的那样,这应该是图像如何存储在照片库中的可靠答案。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-21
        • 1970-01-01
        • 2018-09-19
        • 2011-11-28
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多