In general, objects do not have a single significant “size”, since they can allocate and free any number of other objects as necessary. sizeof(*myObj) gives you the size of a top-level structure, not a very useful number. If you need the full influence of memory on the distribution and use of an object, run in the "Tools" and "Time Allocation" sections.
With a UIImage its practical size is the size of what supports it, usually either NSData containing a PNG , or CGimageRef , plus object overhead. (There is also a pixel buffer when it is displayed on the screen or in another context, but this buffer belongs to the view or context in question, not UIImage . If a UIView renders, then this buffer is probably GL texture memory anyway.)
[UIImage imageWithData:[NSData dataWithContentsOfFile:@"foo.png"]] gives you a UIImage that is the same size as the foo.png file, as well as some minor overhead. [UIImage imageNamed:@"foo.png"] does the same, except that the class maintains a cache table of one object per file name and will force this object to unload its copy of png memory in low memory situations, reducing the "size" to just official.
imageWithCGImage: and the options give you a UIImage that uses the CGImage link as storage, and CGImages can be any number of things, depending on their source. If you paint in one, this is probably an uncompressed pixel buffer. Calculate its size exactly as you suggest above. If you need what its size would "be" if it were from a file, check the result of the UIImagePNGRepresentation or UIImageJPEGRepresentation .
source share