iOS Rendering PDFs on a Background Thread

  • Hi All,

    I like to be able to fire off a Background thread that Renders PDF
    pages to UIImage's.

    I've got the code working on the Main Thread, but now the tricky bit
    because since I am using UIGraphicsBeginImageContextWithOptions and
    UIGraphicsEndImageContext in order to capture the Image, and, as far
    as I know these methods can only be called on the Main Thread. Is
    this correct?

    I can re-code this using CF/CG etc. but I'd like to know if:

    1.  The CF/CG functions are thread safe.

    2.  The CGPDF functions are also thread safe.

    Any help appreciated.

    All the Best
    Dave
  • On Jul 26, 2012, at 2:09 PM, Dave <dave...> wrote:

    > I've got the code working on the Main Thread, but now the tricky bit because since I am using UIGraphicsBeginImageContextWithOptions and UIGraphicsEndImageContext in order to capture the Image, and, as far as I know these methods can only be called on the Main Thread. Is this correct?

    No, these methods are fine as long as you use the context only within the same invocation (that is, don't try to share the context between threads or split the begin/end between threads).
    --
    David Duncan
  • On 26 Jul 2012, at 22:17, David Duncan wrote:

    > On Jul 26, 2012, at 2:09 PM, Dave <dave...> wrote:
    >
    >> I've got the code working on the Main Thread, but now the tricky
    >> bit because since I am using
    >> UIGraphicsBeginImageContextWithOptions and
    >> UIGraphicsEndImageContext in order to capture the Image, and, as
    >> far as I know these methods can only be called on the Main Thread.
    >> Is this correct?
    >
    >
    > No, these methods are fine as long as you use the context only
    > within the same invocation (that is, don't try to share the context
    > between threads or split the begin/end between threads).

    Really? Just double checking you mean what you mean. My code basicall

    UIGraphicsBeginImageContextWithOptions
    UIGraphicsGetCurrentContext
    ---Reander PDF Page into the Context obtained above.
    UIGraphicsGetImageFromCurrentImageContext
    UIGraphicsEndImageContext

    This is all in one method that runs in one thread, are you saying
    this will work ok?

    Thanks a lot for your help.
    Dave
  • On Jul 26, 2012, at 2:39 PM, Dave <dave...> wrote:

    > Really? Just double checking you mean what you mean. My code basicall
    >
    > UIGraphicsBeginImageContextWithOptions
    > UIGraphicsGetCurrentContext
    > ---Reander PDF Page into the Context obtained above.
    > UIGraphicsGetImageFromCurrentImageContext
    > UIGraphicsEndImageContext
    >
    > This is all in one method that runs in one thread, are you saying this will work ok?

    Should be fine. If you find an issue, please report a bug. This was a recent topic at WWDC2012 as well, see session 211 "Building Concurrent User Interfaces on iOS"
    --
    David Duncan
  • Hi,

    Thanks for that!

    All the Best
    Dave

    On 26 Jul 2012, at 22:20, Evadne Wu wrote:

    > Probably not: http://stackoverflow.com/questions/10931155/
    > uigraphicsbeginimagecontextwithoptions-and-multithreading
    >
    > Best,
    > Eve
    >
    > On Jul 27, 2012, at 5:09 AM, Dave <dave...> wrote:
    >
    >> Hi All,
    >>
    >> I like to be able to fire off a Background thread that Renders PDF
    >> pages to UIImage's.
    >>
    >> I've got the code working on the Main Thread, but now the tricky
    >> bit because since I am using
    >> UIGraphicsBeginImageContextWithOptions and
    >> UIGraphicsEndImageContext in order to capture the Image, and, as
    >> far as I know these methods can only be called on the Main Thread.
    >> Is this correct?
    >>
    >> I can re-code this using CF/CG etc. but I'd like to know if:
    >>
    >> 1.  The CF/CG functions are thread safe.
    >>
    >> 2.  The CGPDF functions are also thread safe.
    >>
    >> Any help appreciated.
    >>
    >> All the Best
    >> Dave
    >
  • On 26 Jul 2012, at 22:52, David Duncan wrote:

    > On Jul 26, 2012, at 2:39 PM, Dave <dave...> wrote:
    >
    >> Really? Just double checking you mean what you mean. My code basicall
    >>
    >> UIGraphicsBeginImageContextWithOptions
    >> UIGraphicsGetCurrentContext
    >> ---Reander PDF Page into the Context obtained above.
    >> UIGraphicsGetImageFromCurrentImageContext
    >> UIGraphicsEndImageContext
    >>
    >> This is all in one method that runs in one thread, are you saying
    >> this will work ok?
    >
    >
    > Should be fine. If you find an issue, please report a bug. This was
    > a recent topic at WWDC2012 as well, see session 211 "Building
    > Concurrent User Interfaces on iOS"
    > --
    > David Duncan
    >

    Fantastic!!!
    Thanks a lot!
    Dave
  • On Jul 26, 2012, at 2:09 PM, Dave wrote:

    > Hi All,
    >
    > I like to be able to fire off a Background thread that Renders PDF pages to UIImage's.
    >
    > I've got the code working on the Main Thread, but now the tricky bit because since I am using UIGraphicsBeginImageContextWithOptions and UIGraphicsEndImageContext in order to capture the Image, and, as far as I know these methods can only be called on the Main Thread. Is this correct?

    As David has pointed out, this is incorrect as of iOS (then iPhoneOS) 4.  From the release notes (http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPho
    neOS/Articles/iPhoneOS4.html
    ):

    Drawing to a graphics context in UIKit is now thread-safe. Specifically:
    The routines used to access and manipulate the graphics context can now correctly handle contexts residing on different threads.
    String and image drawing is now thread-safe.
    Using color and font objects in multiple threads is now safe to do.

    I do see that the UIKit documentation has not been updated to reflect this fact.  I suggest filing a documentation bug.

    -Conrad
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today