从磁盘加载 UIImage 有时会失败,并在后台状态下出现权限错误



我有一个奇怪的问题,我无法解决。我正在创建一个允许播放音乐的应用程序。当屏幕锁定(并且当前有一首正在播放的歌曲)时,锁定屏幕将填充一堆数据。一件是专辑封面。

问题是,在手机锁定并且我跳过一些曲目(向前或向后)后,UIImages不再加载。如果我测试该功能并在播放队列中快速向前和向后跳跃,则前 4-5 首歌曲将显示专辑封面。之后,图像停止出现,因为我从抓取图像的代码中得到一个NSFileReadNoPermissionError。我知道我显然没有访问 png 图像文件的权限,但我不明白为什么。我的应用程序创建了它们,将它们保存在磁盘上,现在正在尝试在我的应用程序在后台状态下运行时从磁盘加载它们。

相关代码片段:

+ (void)updateLockScreenInfoAndArtForSong:(Song *)song
{
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *artDirPath = [documentsPath stringByAppendingPathComponent:@"Album Art"];
    NSString *path = artDirPath;
    //-----> LIST ALL FILES for debugging <-----//
    NSLog(@"LISTING ALL FILES FOUND");
    int count;
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path
                                                                                    error:NULL];
    for (count = 0; count < (int)[directoryContent count]; count++)
    {
        NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);
    }
    //-----> END DEBUG CODE <-----//

    Song *nowPlayingSong = [MusicPlaybackController nowPlayingSong];
    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
    if (playingInfoCenter) {
        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
        NSError *error;
        NSData *data = [NSData dataWithContentsOfURL:
                        [AlbumArtUtilities albumArtFileNameToNSURL:nowPlayingSong.albumArtFileName] options:NSDataReadingUncached error:&error];
        NSInteger code = error.code;
        NSLog(@"Error code: %li", (long)code);    //prints 257 sometimes, which is NSFileReadNoPermissionError
        UIImage *albumArtImage = [UIImage imageWithData:data];
        if(albumArtImage == nil){  //song has no album art, check if its album does
            Album *songsAlbum = song.album;
            if(songsAlbum){
                albumArtImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                                        [AlbumArtUtilities albumArtFileNameToNSURL:songsAlbum.albumArtFileName]]];
            }
        }
        [songInfo setObject:nowPlayingSong.songName forKey:MPMediaItemPropertyTitle];
        NSInteger duration = [nowPlayingSong.duration integerValue];
        [songInfo setObject:[NSNumber numberWithInteger:duration]
                     forKey:MPMediaItemPropertyPlaybackDuration];
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
    }
}

任何帮助将不胜感激!我已经尝试了很多东西,以至于我不知道接下来该尝试什么。请注意,上面的代码片段是在

- (void)remoteControlReceivedWithEvent:(UIEvent *)event

当 event.subtype 为 UIEventSubtypeRemoteControlNextTrack 或 UIEventSubtypeRemoteControlPreviousTrack。

在尝试了一整天之后都想通了,哈哈。事实证明,默认情况下,在iOS 8中,大部分系统都是加密的(手机锁定后无法访问文件)...当然有一点延迟。这就是为什么加载了一些专辑封面图像,但它们在几秒钟后停止工作的原因。手机锁定和加密启用本身之间的延迟使我的问题看起来像是"随机的"。

无论如何,对于任何阅读本文的人来说,我的解决方案涉及设置所有文件夹和子文件夹以及指向专辑封面目录的文件的文件保护。

提示:

[attributes setValue:NSFileProtectionCompleteUntilFirstUserAuthentication forKey:NSFileProtectionKey];

创建文件时在文件上设置该属性(以 NSDictionary 的形式提供信息)。如果要修改现有目录或文件,请使用 NSFileManager 收集事物的属性,然后设置如上所示的值。

最新更新