renderincontext Memory Leak if not use on Main thread

后端 未结 2 1670
天命终不由人
天命终不由人 2020-12-30 17:07

I am trying to convert my UIView into UIImage using below code.

+ (UIImage *) imageWithView:(UIView *)view{
     float scale = 1.0f;
    UIGraphicsBeginImag         


        
2条回答
  •  [愿得一人]
    2020-12-30 18:01

    I just had this happen to me (memory leak due to renderInContext) on the main thread. I was looping over hundreds off-screen views, rendering them to UIImage objects, and saving them as PNG files. What solved the problem for me was wrapping the guts of my loop in an @autoreleasepool block:

    Broken:

    for (...) {
       ...render layer in context...
       ...save image to disk...
    }
    

    Works:

    for (...) {
       @autoreleasepool {
          ...render layer in context...
          ...save image to disk...
       }
    }
    

    Makes sense, right?

提交回复
热议问题