我们有大约 2k 个对象,它们是 ALAsset 类的实例,我们需要知道哪些文件是全景图像。
我们尝试从 ALAsset 实例中获取 CGImageRef 并检查宽/高比。

ALAsset *alasset = ...
CFImageRef = alasset.thumbnail; // return square thumbnail and not suitable for me
CFImageRef = alasset.aspectRationThumbnail; //return aspect ration thumbnail, but very slowly

它不适合我们,因为它对许多文件运行缓慢。

我们也尝试从 defaultRepresentation 获取元数据并检查图像 EXIF,但它工作缓慢。
NSDictionary *dictionary = [alasset defaultRepresentation] metadata]; //very slowly to

有没有办法让它更好?

谢谢

最佳答案

最后,我为 ALAsset 找到了这个解决方案:

ALAssetsLibrary *assetsLibrary = ...;
NSOperation *queue = [NSoperationQueue alloc] init];
static NSString * const kAssetQueueName = ...;
static NSUInteger const kAssetConcurrentOperationCount = ...; //I use 5
queue.maxConcurrentOperationCount = kAssetConcurrentOperationCount;
queue.name = kAssetQueueName;
dispatch_async(dispatch_get_main_queue(), ^{
  [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    /*You must check the group is not nil */
    if (!group)
      return;
    /*Then you need to select group where you will search panoramas: for iPhone-Simulator it's @"Saved Photos" and "Camera Roll" for  iPhone. It's actuality only for iOS 7 or early. */
    static NSString * const kAssetGroupName = ...;
    if ([[group valueForProperty:ALAssetsGroupPropertyName] kAssetGroupName]) {
      [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
        if (!asset)
          return;
        [queue addOperationWithBlock:^{
         //I use @autoreleasepool for instant memory release, after I find panoramas asset url
          @autoreleasepool {
            ALAssetRepresentation *defaultRepresentation = asset.defaultRepresentation;
            if ([defaultRepresentation.UTI isEqualToString:@"public.jpeg"]) {
              NSDictionary *metadata = defaultRepresentation.metadata;
              if (!metadata)
                return;
              if (metadata[@"PixelWidth"] && metadata[@"PixelHeight"]) {
                NSInteger pixelWidth = [metadata[@"PixelWidth"] integerValue];
                NSInteger pixelHeight = [metadata[@"PixelHeight"] integerValue];
                static NSUInteger const kSidesRelationshipConstant = ...; //I use 2
                static NSUInteger const kMinimalPanoramaHeight = ...; //I use 600

                if (pixelHeight >= kMinimalPanoramaHeight && pixelWidth/pixelHeight >= kSidesRelationshipConstant) {/*So, that is panorama.*/}
             }

        }];

      }];
    }
  } failureBlock:^(NSError *error) {
      //Some failing action, you know.
  }];
};

那是。所以,我认为这不是最好的解决方案。然而,今天我还没有找到更好的。

关于ios - 如何从 ALAsset 对象获取全景图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24510857/

10-08 20:50