菜鸟教程小白 发表于 2022-12-11 17:06:42

ios - 在 PHPhotoLibrary 中,photoLibraryDidChange 不会通知所有更新


                                            <p><p>我正在使用 <code>PHPhotoLibrary</code> 在 iPhone 中为我的应用程序相册保存和获取图像。</p>

<p>当我的相册有很多图像(大约 5,000 张静止图像)时,
我的应用程序从网络下载了 10 张图片,然后保存到相机胶卷并添加到我的应用程序的相册中。</p>

<p>同时,应用观察 <code>photoLibraryDidChange</code> 回调以知道添加的图像,但它只通知 5 个插入的图像。</p>

<p>我按下 HOME 按钮完成我的申请,并在 Photo App 中检查相机胶卷和我的相册。 <strong>正确有 5010 张图片。</strong></p>

<p>也许 <code>photoLibraryDidChagne</code> 没有通知所有更改?</p>

<p>我的代码如下。</p>

<pre><code>- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
    // dispatch main queue
    dispatch_async(dispatch_get_main_queue(), ^{
      ;
    });
}

- (void)handleChangedLibrary:(PHChange *)changeInstance
{
    PHFetchResultChangeDetails *fetchResultChangeDetails = ;

    if (!fetchResultChangeDetails) {
      NSLog(@&#34;### No change in fetchResultChangeDetails ###&#34;);
      return;
    }

    if (!) {
      ;
      return;
    }

    NSArray *insertedObjects = ;
    if (insertedObjects) {
      for (PHAsset *asset in insertedObjects) {
            if (asset.mediaType == PHAssetMediaTypeImage) {
                NSLog(@&#34;asset=%@&#34;, asset);
                ;
            }
      }
    }

    self.assetsFetchResult = ;
}
</code></pre>

<p>我通过 NSLog 和 Debugger 检查了插入的资源,它确实更新了 5 张图片。</p>

<p>其他 5 张图片没有通知。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我修复了代码中的错误点,并确认修改后问题没有发生。</p>

<p>错误点:</p>

<ol>
<li>在某些情况下,我的代码'return;'在回调中获取 Assets 集合之前。</li>
<li>我应该从 <code>fetchResultChangeDetails.fetchResultAfterChanges</code> 获取新结果,而不是 <code></code></li>
</ol>

<p>我引用了以下 Apple 的代码进行修改。</p>

<p><em>PHPhotoLibraryChangeObserver</em>
<a href="https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibraryChangeObserver_Protocol/" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibraryChangeObserver_Protocol/</a> </p>

<p>我的固定代码如下所示。谢谢。</p>

<pre><code>- (void)handleChangedLibrary:(PHChange *)changeInstance
{
    // Check for changes to the list of assets (insertions, deletions, moves, or updates).
    PHFetchResultChangeDetails *fetchResultChangeDetails = ;
    if (!fetchResultChangeDetails) {
      NSLog(@&#34;### No change in fetchResultChangeDetails ###&#34;);
      return;
    }

    // Get the new fetch result for future change tracking.
    self.assetsFetchResult = fetchResultChangeDetails.fetchResultAfterChanges;

    if (!) {
      ;      
      return;
    }

    NSArray *insertedObjects = ;
    if (insertedObjects) {
      for (PHAsset *asset in insertedObjects) {
            if (asset.mediaType == PHAssetMediaTypeImage) {
                NSLog(@&#34;asset=%@&#34;, asset);
                ;
            }
      }
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 PHPhotoLibrary 中,photoLibraryDidChange 不会通知所有更新,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38591648/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38591648/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 PHPhotoLibrary 中,photoLibraryDidChange 不会通知所有更新