Welcome to my iphone development code blog

Welcome to my iphone development code blog
"SIMPLICITY IS BEST PROFESSION"

Thursday, July 15, 2010

How to access camera or gallery of device iphone/ipod

I've been working on a number of iPhone camera applications. The default photograph functionality seemed kind of awkward to me, and I wanted to have a much simpler process and interface. This is an example of an Xcode project which shows how to load the camera interface with a custom overlaid image, and then wait for a tap anywhere on the screen to take a photo.



To start, I created a new project (a View-based application) called CameraTapper.



I knew I would need to subclass UIImagePickerController, but there's really not much I needed to over-ride. I created a new file (with UIView as a base) called CustomCamera.m (and .h) and then modified them to support the "tap anywhere to capture" functionality:



CustomCamera.h



#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@
interface CustomCamera : UIImagePickerController {

}

@
end


CustomCamera.m



#import "CustomCamera.h"


@
implementation CustomCamera

-
(void) dealloc {
[super dealloc];

}

-
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

// override the touches ended method
// so tapping the screen will take a picture
[self takePicture];
}

-
(void)viewDidDisappear:(BOOL)animated {

[super viewDidDisappear:animated];
}

-
(void) viewDidAppear: (BOOL)animated {

[super viewDidAppear:animated];
}

@
end



I also added an overlay to the project (a 24 bit transparent png):




This overlay would give the user the visual clue of what to do in order to progress to the next step.


The rest of the code is simply there to demonstrate how this could be implemented in another project. I made a simple button within the CameraTapperViewController.m file to load the CustomCamera view, and I added functionality to show how the "taken" photograph could be used after it had been taken -- by adding it to the background of the main view itself.


CameraTapperViewController.h


#import


@interface CameraTapperViewController : UIViewController
{
UIImageView *myImageView;
UIImage *myImage;

}

@end



CameraTapperViewController.m


#import "CameraTapperViewController.h"
#import "CustomCamera.h"


@implementation CameraTapperViewController

- (void) launchCamera {

// Set up the camera

CustomCamera *cameraController = [[CustomCamera alloc] init];
cameraController.sourceType =
UIImagePickerControllerSourceTypeCamera;
cameraController.delegate = self;

cameraController.showsCameraControls = NO;
cameraController.navigationBarHidden = YES;
cameraController.toolbarHidden = YES;

// overlay on top of camera lens view

UIImageView *cameraOverlayView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"camera_overlay.png"]];
cameraOverlayView.alpha = 0.0f;
cameraController.cameraOverlayView = cameraOverlayView;

// animate the fade in after the shutter opens

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:2.2f];
cameraOverlayView.alpha = 1.0f;
[UIView commitAnimations];

[cameraOverlayView release];

[self presentModalViewController:cameraController animated:YES];

}

// User took an image
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)inImage

editingInfo:(NSDictionary *)editingInfo {
myImage = inImage;
myImageView.image = myImage;

// Get rid of the picker interface

[[picker parentViewController]
dismissModalViewControllerAnimated:YES];
[picker release];

}

- (void)viewDidLoad {

// display the background image view
myImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:myImageView];

// display the button to launch the camera

UIButton *btnLaunchCamera = [UIButton

buttonWithType

:UIButtonTypeRoundedRect];
btnLaunchCamera.frame = CGRectMake(60, 220, 200, 40);
[btnLaunchCamera setTitle:@"Launch Camera" forState:

UIControlStateNormal];
[btnLaunchCamera addTarget:self action:@selector(launchCamera) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnLaunchCamera];

[super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

}

- (void)viewDidUnload {
}

- (void)dealloc {

[myImageView release];
[super dealloc];
}

@end



For further exploration, I'd like to start playing with taking a real-time (or near real-time) feed of images and manipulating them -- similar to some of the augmented reality applications that are currently being developed...

How to make settings in iphone

Settings of your application are managed by iphone itself

Step 1: Create a Sample Application

I have used an application similar to the one created in the tutorial presented here (IPhone SDK Hello World).

Step 2: Create a Settings Bundle


Create a new File Command + N and then select Settings from the side menu and Settings  Bundle. The file must be named Settings.bundle to work correctly. 










This will create two new files Root.strings and Root.plist. Open the Root.plist file and you should see the following screen listing some sample settings. This is actually a graphical representation of an XML file which defines the settings and how they are displayed. (You can see the xml code by right clicking the file and selecting Open as Plain Text)




Step 3: Define Settings Schema


The Root element has three child elements (1) Title; which defines the title of this settings view and is set to the name of the application. (2) StringTable; which is not covered in this tutorial. (3) PreferenceSpecifiers which contains all the settings we wish to elicit from the user. PreferenceSpecifiers has type Array and has a child element of type Dictionary for each setting: Item 1, item2 and so forth. We can add items by selecting PreferenceSpecifiers and clicking the Grey button at the end of the row.

Each setting has several properties of type string including the common required properties; Title and Type (Note all strings are case sensitive). The title defines the label displayed beside the control used to modify the setting and type defines the type of control used. There are seven possible controls which can be selected using the Type property:


PSGroupSpecifier

This is the simplest control and has only the two properties defined described above. It is used as a separator to break-up groups of similar settings. It cannot accept any input from the user.


PSTitleValueSpecifier

This displays a "Read Only" setting it cannot accept input from the user but can be used to display settings changed elsewhere.


Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control.  


DefaultValue:

Another String property which can store any text, this defines the Text that will be displayed in the specifier.



PSTextFieldSpecifier

The next type of control is the TextField, it has some additional properties which are listed below


Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the text field.  


DefaultValue:

Another String property which can store any text this defines the initial Text that will be entered into the TextField when it is first loaded.



IsSecure

This is a boolean property which defines if the charecters of the text field will be hidden, set to enabled for password fields.


KeyboardType

A String property which can have one of the following values: Alphabet, NumbersAndPunctuati on, NumberPad, URL, EmailAddress. This property specifies which type of keyboard to display when text field is selected


AutocapitalizationType

A String property which can have one of the following values: None, Sentences, Words, AllCharacters


AutoCorrectionType

A String property which can have one of the following values: Default, No, Yes
 
 

PSSliderSpecifier

This displays a slider on a bar which allows the user to select a number in a specified range. In addition to the standard Title and Type specifiers the Slider has the following properties.

Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control.  

MinimumValue

A property storing a numerical value which corresponds to the slider being on the left end of the bar.

MaximumValue

A property storing a numerical value which corresponds to the slider being on the right end of the bar.

DefaultValue:

 A numerical  property which defines the original position of the slider. 



PSToggleSwitchSpecifier


Displays a toggle switch which can be either On or Off.

Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control. 

TrueValue:

A boolean property which defines the value of the control of the toggle button is in the On position.

FalseValue:

A boolean property which defines the value of the control of the toggle button is in the Off position.

DefaultValue:

A boolean property which defines the position of the Toggle button the first time it is loaded.




PSMultiValueSpecifier

Multi-Value Specifier
Displays a list in a second view and allows the user to select one element from the list.

Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control. 


Values:

A property of type Array which stores sub-elements of type String, each element provides a a pos sible value for the control to take. 


Titles:

A property of type array which stores sub-elements of type String, each element provides a textual representation of one of the values specified in the previous property. For example if the first element of the values array is "01" and the first element of the titles array is "January" then the user is shown January but if January is selected the value stored for the control is 01.  

DefaultValue:

Another String property that defines the initial value of the control, should be one of the elements of the Values list.

PSChildPaneSpecifier  

 

Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control. 

File:

The name of another plist file without the extension.


Step 4: Retrieving Values of Settings

You can retrieve the value of a setting by using the command given below; Replace the hilighted text with the name of the setting you want to get the value of. 

NSString* settingValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"<Setting Key>"]



Step 5: Loading a Child Pane


The final of the Seven Controls allows you to add a sub view. The procedure to do so is to add a new file Command + N and select Other from the menu on the right Property List from the list of fle types.

Create a child prefernces schema exactly as you created the original schema.

No in the original schema add a PSChildPaneSpecifier and add a File item to the specifier with the file name of the child view (less the extension).

Technically this should be enough but as you will see if you run your code now the child view will not load. To load your child view the child plist file has to be inside the settings bundle but I can't find a way of doing this from within XCode (after half an our of random tinkering). So just open a terminal and move the file into the bundle manually. To do this browse to the directory containing your xcode project, and find child.plist file (or whatever you named it). Use mv child.plist Settings.bundle/child.plist to move your file and then click Build and Go in XCode.

If there is some way of moving files into the bundle through XCode that you know of please leave a coment and I will update this document.