When I try to start my drawRect using an image found from a NSUserDefaults default, it crashes the app with a 'EXC_BAD_ACCESS' error. Once I remove the code to a static image value for imagePathB, the code runs with no errors.
Anyone know why my loading the NSUserDefaults would result in this error?
It's hard to see what's wrong from just those 3 lines. Those seem fine, except for the fact that imagePathB is not used.
As a general design point, though, it seems like you're doing more in your drawRect than you should be doing. It seems like you want to decide on an image and load it at some other time than when the system is trying to draw your view.
In the correct version of the code the imagePathB is used, I pasted a code I was still tweaking with. Yeah, in my drawRect I draw my image, place it into a layer, then draw another image and place it into a layer, done. The user may change the image they want at different times, thus having to call drawrect again view setneedsdisplay to redraw the view. I am using this method becuase I need some custom drawing involving multiply modes with colors over images, and this seems the only method of achieving that, since layer effects are not on the iphone, from what the documentation says.
Is there really a sort of limit on how much you should be doing in the draw rect? Should I be sending code to the drawrect instead of the code inside it?
In the correct version of the code the imagePathB is used, I pasted a code I was still tweaking with.
Well, if you need help debugging something, then you need to post the current and correct version of the code. Otherwise how can anyone help you find the bug that is causing the crash?
Quote:
Is there really a sort of limit on how much you should be doing in the draw rect? Should I be sending code to the drawrect instead of the code inside it?
You just want to minimize what you do there. So it seems like you're looking for an loading an image in there. Why not have that preloaded in RAM, for example?
Right, just pointing to the issue the nuserdefaults was killing the app. Anyways, updated the code, and might have found the issue. I was running almost the same code on the init of the viewcontroller, which might have been trying to open and save the value at the same time as drawrect, causing the error. So now its only called in drawrect:
As far as grabbing the image from ram, not sure how to do that. My users will be choosing the image from a tableview, them coming back to this view to see the image they have. But that image also has custom drawing on it, thus why I am calling this drawrect with new image values from time to time.
The app now runs on the device without an issues like before, but I still have to do more testing and turn the animation back onto the CALayer to see if it kills it. Well see...
Well, my point is that checking to see if a key exists in the prefs, and then writing a default value of that key if needed, etc, are things that you don't want to do inside drawRect. All that logic can be done when the view is loaded (or when the user selects something from the tableview).
That really should all be taken care of and an UIImage loaded from flash (like your last line you posted) *before* this point.
Then go about your custom drawing (and nothing else) in drawRect.
Eddie,
I think that all sounds great in theory and the right way, but everytime I try that, its back to the error. For example I define it in the viewcontroller, and then try this instead:
I get the EXC_BAD_ACCESS error. The path is right and it loads the image from userdefaults on load. But right away the app craps out. So, if you cannot run any variables in the drawrect, like a value for the image path, then how do you get an image into drawrect when you want to change?
So, if you cannot run any variables in the drawrect, like a value for the image path, then how do you get an image into drawrect when you want to change?
Just load the UIImage before the drawRect and store it as an instance variable.
Thats what I was saying, is if I load it somewhere else, that just results in a crash because it seems they are both trying to work on that process. But after monitoring and cleaning memory leaks, tweaking code elft and right, I got the ram and leaks all polished and nice now.
So this code works with no problems (for now) at the start of drawRect: