1. Create an empty application and add a view. The XIB design is as follows:
2. when using uiimagepickercontroller, you must specify several very important attributes: Specify the source type (determine whether the source is available before specifying), specify the delegate, and specify whether the image can be edited. After setting, you can start it and "push" it out.
-(Ibaction) takenewphoto :( ID) sender
{
// Create an image Selector
Uiimagepickercontroller * imagepicker = [[uiimagepickercontroller alloc] init];
// Check whether the image source is available before specifying the source type
If ([uiimagepickercontroller issourcetypeavailable: uiimagepickercontrollersourcetypecamera])
{
// Specify the source type
Imagepicker. sourcetype = uiimagepickercontrollersourcetypecamera;
// Before selecting an image, you can simply edit the image to be selected. This includes changing the image selection range by moving up or down, and changing the image size by hand.
Imagepicker. allowsediting = yes;
// Implement the delegate. The delegate must implement the uiimagepickercontrollerdelegate protocol for the user's actions in the image selector.
Imagepicker. Delegate = self;
// After iamgepicker is set, it can be started. Use the following method to "push" the view of the image Selector
[Self presentmodalviewcontroller: imagepicker animated: Yes];
[Imagepicker release];
}
Else
{
Uialertview * Alert = [[uialertview alloc] initwithtitle: Nil message: @ "the camera cannot use" delegate: Nil cancelbuttontitle: @ "Disable" otherbuttontitles: Nil];
[Alert show];
[Alert release];
}
}
3. Use the uiimagepickercontrollerdelegate protocol to perform operations on users.
# Pragma mark-
# Pragma mark uiimagepickercontrollerdelegate protocol Method
// The image is called when you click the "cancel" button in the image selector. This indicates that you want to abort the image selection operation.
-(Void) imagepickercontrollerdidcancel :( uiimagepickercontroller *) picker
{
[Picker dismissmodalviewcontrolleranimated: Yes];
}
// The user is called when he clicks the "choose" button in the selector to notify the delegate object that the selection operation has been completed and the instance of the selected image is returned.
-(Void) imagepickercontroller :( uiimagepickercontroller *) picker didfinishpickingimage :( uiimage *) image editinginfo :( nsdictionary *) editinginfo
{
Noticelabel. Hidden = yes;
[Imageview setimage: Image];
[Picker dismissmodalviewcontrolleranimated: Yes];
}