2015-09-07 205 views
0

使用以下代碼時,它會獲取我的最後一張照片,但它會複製它。它總是得到最後一張照片,但它返回一對:從用戶照片庫獲取最後一張照片返回兩張照片

UIImageView *imageView = (UIImageView *) [self.photoCollectionView viewWithTag:101]; 
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; 
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; 
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions]; 
PHAsset *lastAsset = [fetchResult lastObject]; 
[[PHImageManager defaultManager] requestImageForAsset:lastAsset 
           targetSize:imageView.bounds.size 
           contentMode:PHImageContentModeAspectFill 
           options:nil 
           resultHandler:^(UIImage *result, NSDictionary *info) { 
           NSLog(@"PHImageManager request results %@ and info %@", result, info); 
           dispatch_async(dispatch_get_main_queue(), ^{ 
            [user_photos addObject:result]; 
            NSLog(@"%lu",user_photos.count); 
            [self.photoCollectionView reloadData]; 
            NSLog(@"%lu",user_photos.count); 
           }); 
}]; 

爲什麼會出現這種情況?下面是日誌語句:

2015-09-06 21:48:22.153[41357:11390462] PHImageManager request results <UIImage: 0x7fd1336b8f40>, {60, 40} and info { 
PHImageFileOrientationKey = 0; 
PHImageResultDeliveredImageFormatKey = 4031; 
PHImageResultIsDegradedKey = 1; 
PHImageResultRequestIDKey = 1; 
PHImageResultWantedImageFormatKey = 5003; 
} 
2015-09-06 21:48:22.158[41357:11390462] 2 
2015-09-06 21:48:22.159[41357:11390462] 2 

2015-09-06 21:48:22.166[41357:11390462] PHImageManager request results <UIImage: 0x7fd13374c3b0>, {386, 256} and info { 
PHImageFileOrientationKey = 0; 
PHImageResultDeliveredImageFormatKey = 5003; 
PHImageResultIsDegradedKey = 0; 
PHImageResultRequestIDKey = 1; 
PHImageResultWantedImageFormatKey = 5003; 
} 
2015-09-06 21:48:22.167[41357:11390462] 3 
2015-09-06 21:48:22.167[41357:11390462] 3 

這發生在模擬器上&設備。不使用第三方軟件,我認爲這不重要。爲什麼會被叫兩次?我打電話給它在一個uialertaction塊

回答

0

那麼在做了什麼「PHImageResultIsDegradedKey」做什麼和是什麼的研究後,自然是歸結爲操作系統檢索圖像數據,並得到它後,它調整了它的質量基於設置的輸出,這就是爲什麼我看到第二個圖像。

所以我所做的就是對資產的實際請求之前加入PHImageRequestOptions參數,它僅檢索我想要的照片:

... 
PHAsset *lastAsset = [fetchResult lastObject]; 
PHImageRequestOptions * options = [[PHImageRequestOptions alloc] init]; 
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; 
options.synchronous = NO; 
options.resizeMode = PHImageRequestOptionsVersionOriginal; 
options.networkAccessAllowed = NO; 
[[PHImageManager defaultManager] requestImageForAsset:lastAsset 
          targetSize:imageView.bounds.size 
          contentMode:PHImageContentModeAspectFit 
          options:options 
          resultHandler:^(UIImage *result, NSDictionary *info) { 
          ... 
0

更換PHAsset *lastAsset=[fetchResult lastObject];在你的代碼只是

PHAsset *lastAsset=fetchResult;