I am looking to create thumbnails for a number of diffe开发者_StackOverflow中文版rent document types (mp4, pdf, png, and ppt). I have seen different methods for doing them individually - MPMovieplayerController: requestThumbnailImagesAtTimes or get UIGraphicsGetCurrentContext of the current layer (effectively screen shot).
Is there a better way to get thumbnails of these files?
What is the preferred method of get thumbnails of items? Different method for each?
As far as I'm aware, there isn't a generic way of doing this. I'd love to be proven wrong though.
I have the same requirement in an app I'm currently working on, and wrote the thumbnail generator yesterday. The approach I took was to pass a path to the file and a completion handler block through to the thumbnail generator object.
The thumbnail generator has an NSOperationQueue
that spawns the thumbnail generation process in a background thread and immediately returns a placeholder thumbnail.
When the thumbnail is generated, the thumbnail generator calls the completion handler on the main thread. You'll probably need to use an NSInvocation
object to do this part.
Doing it synchronously results in a noticeable delay if you have more than a couple of thumbnails to generate. Using the placeholder+completion handler block approach means that the UI remains responsive.
It's important to call the completion handler block on the main thread because it will almost certainly be updating your views, which should only ever be done on the main thread. If you don't do this, you'll get some very strange errors, such as scroll views not showing their contents until you scroll them.
You shouldn't really need to use MPMoviePlayerController
to get thumbnails of videos though; AVAssetImageGenerator
is the "Apple-approved" way of doing this; there's an example of how to do this in the AV Foundation Programming Guide.
精彩评论