我想获取相册里的所有图片进入一个NSDictionary里面

NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc]init];

// NSMutableArray* data = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
 {
     [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
      {
          NSString* type = [result valueForProperty:ALAssetPropertyType];
          if ([type isEqualToString:ALAssetTypePhoto])
          {
              ALAssetRepresentation* represent = result.defaultRepresentation;
              CGImageRef imgRef = [represent fullResolutionImage];
              UIImage* image = [UIImage imageWithCGImage:imgRef scale:represent.scale orientation:(UIImageOrientation)represent.orientation];
              NSDictionary* dictionaryURL = [result valueForProperty:ALAssetPropertyURLs];
              NSString* url;
              for (NSString* str in dictionaryURL)
              {
                  url = [dictionaryURL objectForKey:str];
              }
              [dictionary setObject:image forKey:url];
          }
          
      }];
 }
                     failureBlock:^(NSError *error)
 {
     NSLog(@"erro!!!");
 }];
以上是我写的代码,运行之后发现没有办法放进去,调试之后发现应该是函数中的block运行的时候已经是所有函数运行之后了。
只有在所有程序运行完了之后才运行block所以我就没有办法处理NSDictionary里面的照片了,求大神告知解决方法。
有没有方法可以提前调用那个block?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用NSPredicate的Swift代码示例,用于获取相册照片并按时间分组: ```swift struct PhotoItem { let image: UIImage let creationDate: Date } func getPhotosGroupedByTime() -> [[PhotoItem]] { // 获取所有照片 let options = PHFetchOptions() options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] let allPhotos = PHAsset.fetchAssets(with: .image, options: options) // 创建空字典 var groups = [String: [PhotoItem]]() // 使用NSPredicate过滤并分组照片 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" for i in 0..<allPhotos.count { let asset = allPhotos.object(at: i) let creationDate = asset.creationDate ?? Date() let dateString = dateFormatter.string(from: creationDate) let imageManager = PHImageManager.default() let requestOptions = PHImageRequestOptions() requestOptions.isSynchronous = true requestOptions.deliveryMode = .highQualityFormat requestOptions.resizeMode = .fast let predicate = NSPredicate(format: "SELF.dateString == %@", dateString) let filteredArray = (groups as NSDictionary).filtered(using: predicate) if var items = filteredArray.first?.value as? [PhotoItem] { imageManager.requestImage(for: asset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) { (image, _) in if let image = image { items.append(PhotoItem(image: image, creationDate: creationDate)) groups[dateString] = items } } } else { imageManager.requestImage(for: asset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) { (image, _) in if let image = image { groups[dateString] = [PhotoItem(image: image, creationDate: creationDate)] } } } } // 将分组后的照片按时间先后顺序排序 let sortedGroups = groups.sorted { $0.key < $1.key } // 将所有分组添加到一个数组中 var result = [[PhotoItem]]() for (_, items) in sortedGroups { result.append(items) } return result } ``` 在这个示例中,我们定义了一个`PhotoItem`结构体来表示相册照片的图像和创建时间。`getPhotosGroupedByTime`函数首先使用`PHFetchOptions`获取所有照片,并按创建时间排序。然后使用`NSPredicate`过滤并分组照片,我们指定`NSPredicate`的格式为`SELF.dateString == %@`,其中`dateString`是我们根据创建时间计算得到的时间字符串,`%@`是用于替换的占位符。通过对字典进行过滤,我们可以得到一个包含指定时间字符串的数组,然后将新的照片项添加到这个数组中。最后,我们将所有分组添加到一个大数组中,按时间先后顺序排序,并返回结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值