代码之家  ›  专栏  ›  技术社区  ›  Green Ree

如何获得可可中图像的作者

  •  4
  • Green Ree  · 技术社区  · 11 年前

    我不明白为什么metaDic总是空的。 有一个代码。

        CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider(img.CGImage)); //(UIImage *img)
        CGImageSourceRef mySourceRef =  CGImageSourceCreateWithData(dataRef, NULL);
        NSDictionary *metaDic = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(mySourceRef,0,NULL);
        NSDictionary *tiffDic = (NSDictionary *)[metaDic objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
        NSString *AuthorName  =  [tiffDic objectForKey:(NSString *)kCGImagePropertyTIFFArtist];
    

    我做了一些不同的拍照方式。我在这里发现了:

    获取图片及其信息的一种方法-我需要从网站上获取,然后我就可以得到:

                  //  NSURL *UrlPath  - path of picture    image.jpg   from web site
                NSData *dataImg = [NSData dataWithContentsOfURL:UrlPath];
    
                CGImageSourceRef mySource =  CGImageSourceCreateWithData((CFDataRef)dataImg, NULL); 
                NSDictionary *metaDic = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(mySource,0,NULL);
                NSDictionary *tiffDic = [metaDic objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
    
                /// Log of tiffDic    
    tiffDic = {
    Artist =(
      "mr. Smith"
      );
    }
    

    另一种方式-从NSBoudle mainBundle中读取图片:

               // NSURL *NSBundleUrl  -  - path of the same  picture    image.jpg   from [[NSBundle mainBundle] 
                CGImageSourceRef mySource = CGImageSourceCreateWithURL( (CFURLRef) NSBundleUrl, NULL);
                NSDictionary *metaDic = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(mySource,0,NULL);
                NSDictionary *tiffDic = [metaDic objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
    
    /// Log of tiffDic
    tiffDic = {
        Artist = "mr. Smith"; 
    }
    

    当图片数据来自网站时,为什么它会使用大括号作为艺术家姓名的数组?

    2 回复  |  直到 11 年前
        1
  •  7
  •   foundry    11 年前

    您的数据路径如下所示:

    UIImage -> CGImage -> CGDataProvider -> CGImageSource
    

    这是第三步,即清除图像中的元数据。CGDataProviders是一种“较旧”的机制,用于通过“ limited functionality “—这意味着—除其他外—它们不支持元数据。

    试试这样的方法:

    NSData* jpegData = UIImageJPEGRepresentation(image,1.0);
    CFDataRef dataRef = (__bridge CFDataRef)jpegData;
    CGImageSourceRef source = CGImageSourceCreateWithData(dataRef, NULL);
    

    数据路径:

    UIImage -> NS/CFData -> CGImageSource
    

    这将保留元数据。

    如果您使用 UIImage 作为你的起点。 UI图像 去掉了许多可能伴随原始图像源的元数据(TiffDict似乎被精简为 orientation 标签)。您真的想从源中读取“未解释”的数据,并在不读取图像数据的情况下提取元数据(这是使用 CGImageSourceRef ).

    我有 a little test project up on github 它比较了从各种来源提取图像元数据的方法——文件系统、网络URL、资产库、相机,也许你应该看看。

    使现代化 正如Peter所指出的(以及我的项目所显示的),您不应该使用UIImage,而应该使用原始数据源。在您的情况下,它是一个文件系统源,类似于以下内容:

     NSString* path = @"/path/to/resource";
     NSData *data = [NSData dataWithContentsOfFile:path];
     CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    

    或者更好的是(正如Peter再次指出的那样!)你可以使用 CGImageSourceCreateWithURL 跳过它 NSData 一步到位。

        2
  •  1
  •   Peter Hosey    11 年前

    CGImage的数据提供程序提供原始像素,而不是PNG或TIFF或其他包含元数据的外部格式数据。因此,没有可获得的属性。

    如果这个源甚至不能给你一张图像,我也不会感到惊讶,因为它无法知道用什么像素格式来解释数据。

    您需要使用URL或从中获得原始图像的数据来创建图像源,而不是该图像的像素数据。理想情况下,应该先创建图像源,然后从图像源创建图像及其属性。