菜鸟教程小白 发表于 2022-12-12 16:29:17

ios - 从存储在核心数据中的 NSData 播放视频


                                            <p><p>我正在尝试播放存储在核心数据中的视频。获取后显示,有一个对象,objects.video 返回一个值,但 dataString 打印为空。我不确定我可能做错了什么。这是播放视频的正确方式还是我可以做得更好?</p>

<p>我在 Core Data 中有一个对象。</p>

<p>我已将视频作为 NSData 存储在核心数据中。我想获取存储的视频并播放。有没有其他方法可以做到?</p>

<pre><code>_context =[(AppDelegate *)[ delegate] managedObjectContext];
NSFetchRequest *fetchRequest = [ init];
NSEntityDescription *entity = ;
;
// Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [ initWithKey:@&#34;level&#34;
                                                               ascending:YES];
];


NSError *error = nil;
NSArray *fetchResults = ;
if(fetchRequest == nil){
    NSLog(@&#34;Nothing fetched&#34;);
}


for (Activity *objects in fetchResults){

    NSLog(@&#34;%@&#34;,objects.video);
prints-&gt;External Data Reference: &lt;self = 0x7bf48750 ; path = FF54B18E-10B3-4B04-81D4-55AC5E2141B9 ; length = 504426&gt;


NSString *dataString = [ initWithData:objects.video encoding:NSUTF8StringEncoding];
    NSLog(@&#34;%@&#34;,dataString);
    NSURL *movieURL = ;
    NSLog(@&#34;%@&#34;,movieURL);
    _moviePlayer = [ initWithContentURL:movieURL];
    ;
    ;
    ;

}
NSLog(@&#34;%i&#34;,fetchResults.count);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>它只是托管对象的二进制属性。您调用该属性,它会返回 <code>NSData</code>。从那里你可以用 <code>NSData</code> 做任何你想做的事情,而它是内存。您的问题是您无法将 <code>NSData</code> 转换为 <code>NSURL</code>。 <code>NSURL</code> 是对数据的引用,其中 <code>NSData</code> 是实际数据。</p>

<p>您需要做的是将视频文件存储在 SQLite 文件之外的磁盘上。然后在 Core Data 中存储对它的引用(又名 url)。这将允许您将视频文件与电影播放器​​一起使用。</p>

<p>正如其他人所说,将视频存储在 SQLite 文件中是一个<strong>坏主意</strong>。它会破坏 Core Data 的性能。</p>

<h2>更新 1</h2>

<blockquote>
<p>Thanks. I have not saved the video directly to core data instead just copied the video url and saved it as string in core data. Should I create a different folder for the app to store the videos whenever I create a copy of the videos the users use so even if they delete the original video that was uploaded to core data, the copy remains intact and thus maintaining integrity of the Objects in Core Data.</p>
</blockquote>

<p>您的评论不清楚。根据您的问题,您将实际视频存储在核心数据中。根据您显示的输出,您将文件存储在 Core Data 中。你改变了吗?如果是这样,您应该将视频文件存储在已知位置,并将该位置的 <strong>relative</strong> URL 作为字符串存储在核心数据中。然后,当您准备好使用它时,您可以从中构建完整的 URL。由于沙盒可以更改,因此您无法存储整个 URL,因为它会过时。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 从存储在核心数据中的 NSData 播放视频,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32976541/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32976541/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从存储在核心数据中的 NSData 播放视频