Technical Content: Read the album separately and adjust the camera to display the picture on the ImageView
Layout:
1. Create ImageView and button and associate a Pickerimage event for button
[OBJC]View Plaincopy
- <div style="Text-align:left;" ><span style= "font-family: helvetica; -webkit-text-stroke-width: initial;" > self.aimageview = [[ Uiimageview alloc]initwithframe:cgrectmake (6 0, 10 0, 20 0, 20 0)]; </span></div> self.backgroundcolor = [uicolor redColor] ;
- self. Aimageview. userinteractionenabled = YES;
- self. Abutton = [[UIButton Alloc]initwithframe:cgrectmake (98, 350, 12 5, 25)];
- self. Abutton. backgroundcolor = [Uicolor bluecolor];
- [self. Abutton addTarget: The self action:@selector (pickerimage:) forcontrolevents: ( UIControlEventTouchUpInside)];
- [self. Abutton settitle:@ "Select Image" forstate: (UIControlStateNormal)];
- [self. View addsubview:selfAbutton];
- [self. View addsubview:selfaimageview];
- [self. Abutton release];
- [self. Aimageview release];
- [self addtapgestureonimageview];
2. Add a tap action on ImageView because some scenes need to be changed directly by clicking on the image
[OBJC]View Plaincopy
- -(void) addtapgestureonimageview{
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initwithtarget:self action:@ Selector (pickerimage:)];
- [self. Aimageview Addgesturerecognizer:tap];
- [Tap release];
- }
3. Implement the method in the Pat action
[OBJC]View Plaincopy
- -(void) Pickerimage: (UIButton *) button{
- //Add Actionsheet control, Prompt for options box, bring up camera or take picture
- ///First parameter: is the title of the behavior list is generally empty
- ///second parameter: Follow the agent
- ///Third parameter: Cancels the text displayed on this action button
- ///Fourth parameter: destructive destructive, devastating self-understanding anyway, I wrote about taking pictures, doing the action.
- ///Fifth parameter: Select a picture from the album
- Uiactionsheet *actionsheet = [[Uiactionsheet alloc]initwithtitle: Nil delegate: Self Cancelbuttontitle:@ "Cancel" Destructivebuttontitle:@ "Take photo" Otherbuttontitles:@ "select Picture from album", Nil Nil];
- //Display the Actionsheet object in the current interface
- [Actionsheet Showinview:self. view];
- [Actionsheet release];
- }
4. Implementing methods in the Action Proxy protocol
[OBJC]View Plaincopy
- -(void) Actionsheet: (uiactionsheet *) Actionsheet clickedbuttonatindex: (nsinteger) buttonindex{
- switch (buttonindex) {
- Case 0:
- //Call system camera, take photo
- [self Pickerpicturefromcamera];
- Break ;
- case 1:
- [self pickerpicturefromphotosalbum];
- Default:
- Break ;
- }
- }
4.1 Getting pictures from the camera
[OBJC]View Plaincopy
- -(void) pickerpicturefromcamera{
- //To determine if the front camera is available, if not available, use the rear camera. If the rear camera is not available, use the phone's picture library
- //Determine if the front-facing camera is available
- if ([Uiimagepickercontroller Iscameradeviceavailable:uiimagepickercontrollercameradevicefront]) {
- NSLog (@ "front-facing camera");
- self. imagepc = [[Uiimagepickercontroller alloc]init];
- self. Imagepc. cameradevice = Uiimagepickercontrollercameradevicefront;
- [self. IMAGEPC release];
- //Determine if the rear-facing camera is available
- }Else if ([Uiimagepickercontroller iscameradeviceavailable:uiimagepickercontrollercameradevicerear]) {
- NSLog (@ "Rear-facing camera");
- self. imagepc = [[Uiimagepickercontroller alloc]init];
- self. Imagepc. cameradevice = uiimagepickercontrollercameradevicerear;
- [self. IMAGEPC release];
- //If neither of them is possible, take photos from the mobile album
- }else{
- Uialertview *alertview = [[Uialertview Alloc]initwithtitle:@ ' call camera failed ' message:@ ' Please select photos from your phone album ' delegate:self cancelbuttontitle:@ "OK" otherbuttontitles: nil, nil nil];
- [Alertview show];
- [Alertview release];
- }
- //Initialize Picture Controller object
- Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc]init];
- //sourcetype Resource Styles
- //Set Picture selector to select a picture's style
- Imagepicker. sourcetype = uiimagepickercontrollersourcetypephotolibrary;
- //Set whether the picture allows editing
- Imagepicker. allowsediting = YES;
- //Set Picture Selector proxy object for this view controller
- Imagepicker. Delegate = self ;
- //Push the camera out to modal
- [self presentviewcontroller:imagepicker animated:YES completion: nil];
- //Release
- [Imagepicker release];
- }
4.2 Getting pictures from your phone's picture library
[OBJC]View Plaincopy
- -(void) pickerpicturefromphotosalbum{
- //Initialize Picture Controller object
- Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc]init];
- //sourcetype Resource Styles
- //Set Picture selector to select a picture's style
- Imagepicker. sourcetype = uiimagepickercontrollersourcetypephotolibrary;
- //Set whether the picture allows editing
- Imagepicker. allowsediting = YES;
- //Set Picture Selector proxy object for this view controller
- Imagepicker. Delegate = self ;
- //The selector controller is introduced to the modal
- [self presentviewcontroller:imagepicker animated:YES completion: nil];
- //Release
- [Imagepicker release];
- }
5. How to save selected photos to the details page
[OBJC]View Plaincopy
- -(void) Imagepickercontroller: (uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: ( nsdictionary *) info{
- //Use a photo album as a dictionary to remove photos from
- self. Aimageview. image = [info objectforkey:uiimagepickercontrollereditedimage];
- //Frame the marquee back
- [self dismissviewcontrolleranimated:YES completion: nil];
- }
Read albums in iOS, call system camera technology share