How to fix terminated due to memory issue when using UIGraphicsGetImageFromCurrentImageContext?
Use this may be it help you.
It help me in my case
@autoreleasepool {
// Write your code here
}
UIGraphicsGetImageFromCurrentImageContext and Swift Memory Usage
We found out that function
func combineImages()
was almost innocent for our memory problem. When we are trying to pinpoint issue about that function, we change the place where we call that function.
We were calling this function from our movie creation loop just before adding the new image to movie.
Now we are creating images using combineImages before movie creation loop started.
When we have been creating combined images we noted that there is no memory usage increase at all.
Besides, the memory usage increase when creating movie file is reduced.
renderincontext Memory Leak if not use on Main thread
I think that issue 1 is related to the fact that UIKit
is not thread safe and its use will lead to all kinds of side effects.
If you have performance issue like you are describing, the only path forward I see is directly using CoreGraphics (not UIKit) on a secondary thread.
You might try something like this, as a start:
size_t width = view.bounds.size.width;
size_t height = view.bounds.size.height;
unsigned char *imageBuffer = (unsigned char *)malloc(width*height*4);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext =
CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGColorSpaceRelease(colourSpace);
[view.layer renderInContext:imageContext];
CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
CGImageRelease(outputImage);
CGContextRelease(imageContext);
free(imageBuffer);
As you see, this is pretty more complex than the UIKit
way, but it can be run on a secondary thread (provided you find a way to pass the outputImage
back to your UI thread which is not shown).
Related Topics
What Is the Height of Iphone's Onscreen Keyboard
How to Rotate Text for Uibutton and Uilabel in Objective-C
Invalid Update: Invalid Number of Rows in Section 0
How to Make an Uipickerview Component Wrap Around
What's the Best Way to Find the User's Documents Directory on an Iphone
Testing Corelocation on iPhone Simulator
Insert CSS into Loaded HTML in Uiwebview/Wkwebview
Xcodebuild Different Provisioning Profile for Target Dependency
Swift Filter Dictionary Error: Cannot Assign a Value of Type '[(_, _)]' to a Value of Type '[_:_]'
How to Use Iboutletcollection to Connect Multiple Uiimageviews to the Same Outlet
Nspersistentcontainer Concurrency for Saving to Core Data
Exhaustive Condition of Switch Case in Swift
How to Open File and Append a String in It, Swift
Getting Data Out of a Closure That Retrieves Data from Firebase
Custom Init for Uiviewcontroller in Swift with Interface Setup in Storyboard
How to Change Device Volume on iOS - Not Music Volume
Valid File Path for Archiverootobject and Unarchiverootobject