2017-03-02 82 views
0

这已弃用,可能是更新的代码是什么?不推荐使用ALAssetsLibrary方法

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
    [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) { 
     ALAssetRepresentation *rep = [asset defaultRepresentation]; 
+0

看到这个http://stackoverflow.com/questions/33500266/how-使用-phphotolibrary-like-alassetslibrary –

+1

这是整个框架已被弃用。使用'Photos.framework' – Larme

+0

非常感谢,明白了。 – Ren

回答

1

使用以下代码从图库中获取所有图片: 首先您需要导入Photo框架。越来越像之前

#import <Photos/Photos.h> 

采取授权:

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) 
{ 
    switch (status) { 
     case PHAuthorizationStatusAuthorized: 
      [self performSelectorOnMainThread:@selector(getAllPictures) withObject:nil waitUntilDone:NO]; 
      // [self getAllPictures]; 
      NSLog(@"PHAuthorizationStatusAuthorized"); 
      break; 
     case PHAuthorizationStatusRestricted: 
      NSLog(@"PHAuthorizationStatusRestricted"); 
      break; 
     case PHAuthorizationStatusDenied: 
      NSLog(@"PHAuthorizationStatusDenied"); 
      break; 
     default: 
      break; 
    } 
}]; 

-(void)getAllPicture 
{ 
      NSLog(@"Started..."); 
      PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init]; 
      requestOptions.synchronous = YES; 
      PHFetchOptions *allPhotosOptions = [PHFetchOptions new]; 
      allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; 

      PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions]; 
      for (PHAsset *asset in result) { 

       NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; 
       [dic setValue:asset forKey:@"assest"]; 
       [YOUR_ARRAY insertObject:dic atIndex:0]; 
       dic = nil; 
      } 
      NSLog(@"Completed..."); 
} 

您可以从下面的代码retrive图像:

PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init]; 
requestOptions.synchronous = YES; 

PHImageManager *manager = [PHImageManager defaultManager]; 
[manager requestImageForAsset:YOUR_ARRAY[INDEX_ARRAY][@"assest"] 
        targetSize:CGSizeMake(self.view.frame.size.width/3, 200) 
        contentMode:PHImageContentModeDefault 
         options:requestOptions 
       resultHandler:^void(UIImage *image, NSDictionary *info) { 
        YOUR_IMAGE_VIEW.image = image; 
       }];