2016-04-14 4 views
2

Das Abrufen von PHAssets stürzt jetzt bei den neuesten Versionen von iOS ab (iOS 9.2 und iOS 9.3). Es hat vorher gut funktioniert.[PHCollectionList canContainCustomKeyAssets]: unerkannter Selektor an Instanz gesendet

Der Fehler Ich erhalte ist:

[PHCollectionList canContainCustomKeyAssets]: Unbekannter Selektor an Instanz gesendet App beenden aufgrund nicht abgefangene Ausnahme 'NSInvalidArgumentException'

Die Linie, die Ausnahme zu werfen ist:

  PHFetchResult *fetchImage = [PHAsset fetchKeyAssetsInAssetCollection:(PHAssetCollection*)collection options:fetchOptions]; 

Hier ist mehr Code, als Referenz:

Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary"); 
     if (PHPhotoLibrary_class) { 

      PHFetchResult *fetchResult = self.collectionsFetchResults[indexPath.section]; 
      PHCollection *collection = fetchResult[indexPath.row]; 

      cell.textLabel.text = collection.localizedTitle; 

      PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; 
      fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; 
      PHFetchResult *fetchImage = [PHAsset fetchKeyAssetsInAssetCollection:(PHAssetCollection*)collection options:fetchOptions]; 
      PHAsset *asset = [fetchImage firstObject]; 

      PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; 
      options.resizeMode = PHImageRequestOptionsResizeModeExact; 

      CGFloat scale = [UIScreen mainScreen].scale; 
      CGFloat dimension = 90.0; 
      CGSize size = CGSizeMake(dimension*scale, dimension*scale); 

      [[PHImageManager defaultManager] requestImageForAsset:asset 
                 targetSize:size 
                 contentMode:PHImageContentModeAspectFill 
                  options:options 
                resultHandler:^(UIImage *result, NSDictionary *info) { 
                 if (result) { 
                  CGSize itemSize = CGSizeMake(60, 60); 
                  UIGraphicsBeginImageContextWithOptions(itemSize, NO, 2); 
                  CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); 
                  [result drawInRect:imageRect]; 
                  cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
                  UIGraphicsEndImageContext(); 
                 } 
                 else{ 
                  UIImage *placeholder = [UIImage imageNamed:@"image-placeholder.jpg"]; 
                  CGSize itemSize = CGSizeMake(60, 60); 
                  UIGraphicsBeginImageContextWithOptions(itemSize, NO, 2); 
                  CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); 
                  [placeholder drawInRect:imageRect]; 
                  cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
                  UIGraphicsEndImageContext(); 
                 } 
                }]; 
     } 

Antwort

1

Dieses Problem mit hier eigentlich liegt:

PHCollection *collection = fetchResult[indexPath.row]; 

und dann hier:

PHAsset *asset = [fetchImage firstObject]; 

Sie holen die Sammlung PHCollection und anschließend unter der Annahme, nur alle Vermögenswerte sind PHAssets ohne richtig zu überprüfen, ob dies der Fall ist oder nicht.

Eigentlich PHCollection hat two possible subclasses: PHAsset und PHCollectionList und die PHCollectionList ist das, was hier den Fehler zu werfen.

Wickeln Sie den Code nach PHCollection *collection = fetchResult[indexPath.row]; mit einem Scheck für PHAsset, und es soll das Problem lösen:

if ([collection isKindOfClass:[PHAssetCollection class]]) { 
//code 
} 
0

Zunächst erhalten alle Vermögenswerte PHAsset wie

dispatch_async(dispatch_get_main_queue(), ^{  

     AllPhotos=[[NSMutableArray alloc] init]; 
     PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil]; 
     for (PHAsset *asset in result) 
     { 
      [AllPhotos addObject:asset]; 

     } 
     [self.collectionView reloadData]; 

     // code here 
    }); 

nicht, verwenden Sie die Kollektion Delegatmethode wie

PHImageManager *manager = [PHImageManager defaultManager]; 
    PHAsset *asset = [AllPhotos objectAtIndex:indexPath.row]; 
    PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init]; 
    requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact; 
    requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic; 
    requestOptions.synchronous = false; 

    //CGFloat scale = 1.5; //or an even smaller one 
    if (!manager) 
    { 
     manager = [[PHCachingImageManager alloc] init]; 
    } 
    if (!requestOptions) 
    { 
     requestOptions = [[PHImageRequestOptions alloc] init]; 
    } 

    [manager requestImageForAsset:asset 
         targetSize:CGSizeMake(imagesize.width,imagesize.height) 
         contentMode:PHImageContentModeAspectFill 
          options:requestOptions 
        resultHandler:^void(UIImage *image, NSDictionary *info) 
    { 
     [imageview setImage:image]; 
    }]; 

    [cell.contentView addSubview:imageview]; 
Verwandte Themen