Working with Media, iPhone




CS185c

Chris Pollett

Dec 1, 2010

Outline

Multimedia on iPhone

Audio app

The XIB file and Implementation

VideoDemoViewController.m

The Camera and Photo Library

CameraIPhoneViewController.h

//
//  CameraIPhoneViewController.h
//  CameraIPhone
//
//  Created by Chris Pollett on 12/1/10.
//  Copyright 2010 San Jose State University. All rights reserved.
//

#import 

@interface CameraIPhoneViewController : UIViewController 
  {
	 IBOutlet UIImageView *imageView;
	 IBOutlet UIButton *takePictureButton;
	 IBOutlet UIButton *selectFromCameraRollButton;
}
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIButton *takePictureButton;
@property (nonatomic, retain) UIButton *selectFromCameraRollButton;
- (IBAction)getCameraPicture:(id)sender;
- (IBAction)selectExistingPicture;
@end

The XIB file

CameraIPhoneViewController.m

//
//  CameraIPhoneViewController.m
//  CameraIPhone
//
//  Created by Chris Pollett on 12/1/10.
//  Copyright 2010 San Jose State University. All rights reserved.
//

#import "CameraIPhoneViewController.h"

@implementation CameraIPhoneViewController
@synthesize imageView;
@synthesize takePictureButton;
@synthesize selectFromCameraRollButton;

- (void)viewDidLoad {
   if(![UIImagePickerController 
		isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
	   takePictureButton.hidden = YES;
	   selectFromCameraRollButton.hidden = YES;
   }
}

 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}


- (void)dealloc {
	[imageView release];
	[takePictureButton release];
	[selectFromCameraRollButton release];
    [super dealloc];
}

#pragma mark -
-(IBAction)getCameraPicture: (id)sender {
	UIImagePickerController *picker = [[UIImagePickerController alloc] init];
	picker.delegate = self;
	picker.allowsImageEditing = YES;
	picker.sourceType = (sender == takePictureButton) ?
		UIImagePickerControllerSourceTypeCamera :
		UIImagePickerControllerSourceTypeSavedPhotosAlbum;
	[self presentModalViewController:picker animated: YES];
	[picker release];
}

-(IBAction)selectExistingPicture {
	if([UIImagePickerController isSourceTypeAvailable:
		UIImagePickerControllerSourceTypePhotoLibrary]) {
		UIImagePickerController *picker = 
			[[UIImagePickerController alloc] init];
		picker.delegate = self;
		picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
		[self presentModalViewController:picker animated:YES];
		[picker release];
	} else {
		UIAlertView *alert = [[UIAlertView alloc]
			initWithTitle:@"Error accessing photo library" 
			message:@"Device does not support a photo library" 
			delegate:nil 
			cancelButtonTitle:@"Darn!" 
							  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}

-(void)imagePickerController:(UIImagePickerController *)picker
		didFinishPickingImage:(UIImage *)image
				 editingInfo:(NSDictionary *)editingInfo {
	imageView.image = image;
	UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
	[picker dismissModalViewControllerAnimated:YES];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
	[picker dismissModalViewControllerAnimated:YES];
}

@end