ios - 如何从 ALAsset 对象获取全景图

标签 ios objective-c ios7

我们有大约 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/

相关文章:

ios - 将按钮添加到 UIPickerView - Swift 1.2

ios - google+ 如何保护嵌入在 ios 应用程序中的 api-key?

html - 无法在 Cocoa 的 HTML webview 中设置表格的最大宽度

ios - UINavigationBar 元素太靠近 UIStatusBar

objective-c - 如何使我的导航栏统一半透明?

iphone - UILocalNotification 没有出现在 iOS7 中

ios - 从 HTML 字符串 wkwebview 加载脚本

ios - UIStoryboard 更改标签栏按钮文本

ios - 如何根据核心数据中的一个字段获得不同的结果

iphone - 应用程序仅在iPhone设备上崩溃而不在模拟器中崩溃