Professional Documents
Culture Documents
More Views
Autorelease
User Defaults
Today’s Topics
• Questions from previous lectures or assignments?
• More view topics
■ Additional view types
• Advanced memory management - autorelease
■ aka, “why don’t I have to release that object?”
• User Defaults
■ Easily store and retrieve small bits of data
More View Stuff
Constructing Views
• What’s the right way to
implement this?
• Goal
■ Single PolygonView that displays
shape as well as name
Constructing Views
• What’s the right way to
implement this?
• Goal
■ Single PolygonView that displays
shape as well as name
• Initial thought
■ Simply have PolygonView draw
the text
■ Inefficient if you want to animate
- (CGSize)sizeWithFont:(UIFont *)font
minFontSize:(CGFloat)minSize
actualFontSize:(CGFloat *)actualSize
forWidth:(CGFloat)width
lineBreakMode:(UILineBreakMode)mode;
@end
• Minor differences
■ No superclass declaration
■ No instance variables
Interface Declaration
In UIStringDrawing.h
Category name
#import <Foundation/NSString.h>
- (CGSize)sizeWithFont:(UIFont *)font;
- (CGSize)drawAtPoint:(CGPoint *)point withFont:(UIFont *)font;
@end
Implementation Declaration
In UIStringDrawing.m
Category name
#import “UIStringDrawing.h”
- (CGSize)sizeWithFont:(UIFont *)font {
....
}
- (CGSize)drawAtPoint:(CGPoint *)point withFont:(UIFont *)font {
....
}
@end
Images
• UIImage
■ Object representation of an image
• UIImageView
■ A view that contains an image, displays a UIImage
Creating Images
• Include in project using Xcode
■ Use +[UIImage imageNamed:(NSString *)name]
■ Include file extension in file name, e.g. @”myImage.jpg”
UIImage *myImage;
• Drawing as pattern
UIImage * myImage = [UIImage imageNamed:@"myImage.jpg"];
UIGraphicsBeginImageContext (size);
// call your drawing code...
result = UIGraphicsGetImageFromCurrentContext();
UIGraphicsEndImageContext();
return result;
}
Getting Image Data
• Given UIImage, want PNG or JPG representation
NSData *UIImagePNGRepresentation (UIImage * image);
NSData *UIImageJPGRepresentation (UIImage * image);
• UIImage also has a CGImage property which will give you a
CGImageRef to use with CG calls
User Defaults
User Defaults
• Simple system for storing and retrieving preferences
• Only used for “small” amounts of data
■ Don’t store things like documents or images here
• What can you store in defaults?
■ Any property list types
■ NSString, NSArray, NSDictionary, NSNumber, NSDate, NSData
- (id)objectForKey:(NSString *)key;
- (BOOL)boolForKey:(NSString *)key;
- (int)integerForKey:(NSString *)key;
- (float)floatForKey:(NSString *)key;
User Defaults Example - Saving
• At an appropriate time, save the changed values
■ Commonly right where the change happens
■ Alternatively in delegate’s applicationWillTerminate: method
■ Be mindful of performance
- (IBAction)showAdvancedOptions {
NSUserDefaults *defaults;
BOOL isOn = advancedOptions.isOn;
- (void)applicationDidFinishLaunching:(NSNotification *)note {
NSUserDefaults *defaults;
BOOL isOn = NO;
return result;
}
Problem Cases
- (NSString *)fullName {
NSString *result;
return result;
}
Wrong: result is leaked!
Problem Cases
- (NSString *)fullName {
NSString *result;
[result release];
return result;
}
Problem Cases
- (NSString *)fullName {
NSString *result;
[result release];
return result;
}
return result;
}
Problem Cases
- (NSString *)fullName {
NSString *result;
[result autorelease];
return result;
}
Problem Cases
- (NSString *)fullName {
NSString *result;
[result autorelease];
return result;
}
+ (NSMutableString *)string {
return [[[self alloc] init] autorelease];
}
What Happens In autorelease?
• Receiving object is added to current autorelease pool
• Autorelease pools collect objects scheduled to receive release
■ when pool itself is released, it sends release to all objects in pool
• UIKit wraps pool around every event dispatch
AutoreleasePools (in pictures)
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
Objects autoreleased
here go into pool
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
Objects autoreleased
here go into pool
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
Objects autoreleased
here go into pool
[object
autorelease];
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
Objects autoreleased
here go into pool
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
Objects autoreleased
here go into pool
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
Objects autoreleased
here go into pool
Pool released
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
[object release];
Objects autoreleased
[object release]; here go into pool
[object release];
Pool released
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
AutoreleasePools (in pictures)
Objects autoreleased
here go into pool
Pool released
Pool created
a pp ed ib t t p
h i z n en en p
nc t ia l
ain re
v e v it a
Lau ni fo le Ex
p i
adm i t nd
Ap Lo Wa Ha
autorelease Conventions
• Methods that aren’t alloc/copy return autoreleased objects
■ They’re hanging out in the pool and will get a release later
• If you need to hold onto those objects you need to retain them
■ Bumps up the retain count before the release happens
Manual Autorelease Pools
• In some cases where lots of objects are created and
autoreleased quickly, may want a local pool
• Pools nest and objects get put into most recent pool
• Reduced peak memory usage
NSAutoreleasePool *pool;
while (10000) {
pool = [[NSAutoreleasePool alloc] init];
[pool release];
}
Questions?