Online Book Reader

Home Category

iPhone Game Development - Chris Craft [124]

By Root 1613 0
application


The actual listing for the header of PhotoPicker can be found in the source in the file PhotoPickerController.h. As you've seen, the PhotoPickerController is a convenient wrapper that minimizes the code needed to select photos in your application:

#import

@interface PhotoPickerController : UIImagePickerController

- (id) initWithSourceType:(UIImagePickerControllerSourceType)sourceType;

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;

- (void)imagePickerController:(UIImagePickerController *)picker

didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;

- (void)navigationController:(UINavigationController *)navigationController

willShowViewController:(UIViewController *)viewController

animated:(BOOL)animated;

- (void)navigationController:(UINavigationController *)navigationController

didShowViewController:(UIViewController *)viewController

animated:(BOOL)animated;

@end

The implementation for this class can, of course, be found in the same source, in the file PhotoPickerController.m. There you see UIImagePickerController and how it is used to capture the image data. Notice the sourceType that is passed in and notice the delegate has been set to self:

#import “PhotoPickerController.h”

#import “MainViewController.h”

@implementation PhotoPickerController

- (id) initWithSourceType:(UIImagePickerControllerSourceType)sourceType {

if (self = [super init]) {

if ([UIImagePickerController: isSourceTypeAvailable:

UIImagePickerControllerSourceTypePhotoLibrary])

self.sourceType = sourceType;

self.delegate = self;

self.allowsImageEditing = TRUE;

self.navigationBar.barStyle = UIBarStyleBlackTranslucent;

}

return self;

}

Since we assigned self as the delegate above, the message imagePickerController is sent to the same class whenever a picture has been selected. This message returns the selected image, and in this implementation we store the image to disk:

- (void)imagePickerController:(UIImagePickerController *)picker

didFinishPickingImage:(UIImage *)image

editingInfo:(NSDictionary *)editingInfo {

NSString *uniquePath = [[NSHomeDirectory

stringByAppendingPathComponent:@”Documents”]

stringByAppendingPathComponent:@”selectedImage.png”];

[UIImagePNGRepresentation(image) writeToFile: uniquePath atomically:YES];

[self.parentViewController dismissModalViewControllerAnimated:YES];

[[MainViewController getInstance] loadImage];

}

Note

In the previous code, the word atomically in the method UIImagePNGRepresentation:writeToFile: uniquePath atomically: may look like a typo, but it's not. If you set this value to YES, you are guaranteed that the path, if it exists at all, won't be corrupted even if your device should crash during writing.

Later, when you return to the main screen, this image is loaded from disk and displayed in the photo frame (Figure 9.6).

FIGURE 9.6

After the user selects a photo, it is presented in a simulated photo frame.


Finally, the message imagePickerControllerDidCancel is sent in the event that the user clicks Cancel in the image picker and does not select a picture. Put code in this method when you wish to take special action in this situation:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[self.parentViewController dismissModalViewControllerAnimated:YES];

}

Getting Oriented with the Compass

The iPhone 3GS originally came equipped with a compass (Figure 9.7) containing heading information on magnetic north, true north, and accuracy. You can also take advantage of a 3-D vector that reports the device's deviation from the magnetic lines the compass is tracking.

FIGURE 9.7

The compass application was first introduced with the release of the iPhone 3GS.


The compass is bundled in Core Location as part of the CLLocationManager class. If you

Return Main Page Previous Page Next Page

®Online Book Reader