views:

20

answers:

1

Hello, I'm loading a very big file in an NSimage with this code :

[[NSImage alloc] initWithContentsOfFile:aFile]

This operation take a few time. I want to display the loading status on my UI. How can it's possible to read or calculate a progression ?

Thanks.

+1  A: 

Initialize the image by referencing the file, which will not load it immediately. Then, set yourself as the image's delegate and respond to the incremental-loading messages that are part of the NSImageDelegate protocol. Then, attempt to ask the image for some information about itself (asking for its representations would probably be a good way), to cause the image to start loading.

I think this will still block your UI, though: You'll be able to display progress, but not to enable the user to work on other things while the image loads. I'm not sure how you would do that.

Peter Hosey
Wouldn't it be possible to pack the loading process in a seperate thread? The thread may then post notifications on it's progress from time to time to which the app may react and update other UI Elements. Add the image object to the view stack after it has been loaded completely.As long as the NSImage object is not part of the view stack and the main thread, I don't know why it should block anything.
Toastor
I would expect the `representations` (or whatever) message to block until the image has fully loaded, which is the earliest it will have the complete value to return. You're right that you could do it from a thread, but I can't shake the vague feeling that one of the other APIs (maybe CGImageSource?) will let you load the image asynchronously without spawning off a thread yourself.
Peter Hosey
i have test this idea, but my delegate is never call.It's very strange.
Pixman
Try asking for something else, then, such as its size.
Peter Hosey