Sunday, February 13, 2011

iPhone dev Stupidity 143: Size of Image Cut from CGContext

UIGraphicsBeginImageContext(scale_rect.size);
[self drawInRect: scale_rect];

UIImage * thumbnail = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGContext will always round to ceiling - trying to hold all drawings.

That means: scale_rect.size.width == 320.00000031 will be rounded to 321. If your app care that 1 point, it’ll cause error.

Friday, February 11, 2011

iPhone dev Stupidity 142: Silencing deprecation warnings

from here:
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

Wednesday, February 02, 2011

iPhone dev Stupidity 141: applicationWillTerminate: in iOS4 & iOS3

From the iPhone Application Programming Guide:
Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. If memory becomes constrained, the system might remove applications from memory in order to make more room. If your application is currently suspended, the system removes your application from memory without any notice. However, if your application is currently running in the background, the system does call the applicationWillTerminate:method of the application delegate. Your application cannot request additional background execution time from this method.
So yes, applicationWillTerminate: will generally not be called very often in iOS 4. If you have to save data, you should do so in both applicationWillTerminate: andapplicationDidEnterBackground:

Friday, January 28, 2011

iPhone dev Stupidity 140: encode CGRect into NSUserDefaults

from stackoverflow:
There exists a pair of functions NSStringFromCGPoint and CGPointFromString. You can use these to produce an array of strings representing the points for serialization, and then convert back when you're finished.

Tuesday, January 18, 2011

Monday, January 17, 2011

iPhone dev Stupidity 137: Understanding instruments allocations


ObjectAlloc tracks all memory allocation and deallocation over the time your program is running.
The Living bytes, or Net bytes is how much memory your application is using at the time you select in the timeline. That will include leaked memory, since leaked memory is never deallocated.

#Living is how many allocations of a certain size/object type happened (and are still allocated). This is very useful when looking for leaks. For example, if you repetitively perform an action (like coming in an out of a modal view controller), and you see that #Living of an object grows by the same amount each time, then you’re probably leaking those objects. You can then confirm by drilling down and seeing the exact line of code that is allocating the objects, and even see the time index each one was created.

Overall bytes includes memory that has been released. It’s useful to track that number for performance optimization purposes, but not if you’re just trying to see your current memory footprint or look for leaks.

http://stackoverflow.com/questions/2154219/instruments-objectalloc-explanation-of-live-bytes-overall-bytes

Saturday, January 15, 2011

iPhone dev Stupidity 136: Two stage animation warning

Since iOS 4.0 you got this warning when present a camera picker controller:

Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.

The real cause is from stackoverflow:
This message will appear if you have are presenting the UIImagePickerController within another UIViewController. Because it isn't pushed like a UINavigationController stack, there is confusion at the UIWindow level. I don't know if the warning is a problem, but to eliminate the warning you can do the following:

// self = a UIViewController
//

- (void) showCamera
{
cameraView = [[UIImagePickerController alloc] init];
[[[UIApplication sharedApplication] keyWindow] setRootViewController:cameraView];
[self presentModalViewController:cameraView animated:NO];
}

- (void) removeCamera
{
[[[UIApplication sharedApplication] keyWindow] setRootViewController:self];
[self dismissModalViewControllerAnimated:NO];
[cameraView release];
}

There’s also a good point for iOS 4.x:

Perhaps you are adding the root UIViewController's view as a subview of the window instead of assigning the view controller to the window's rootController property?