Read albums in iOS, call system camera technology share

Source: Internet
Author: User
Tags vars

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
  1. <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] ;   
  2. self. Aimageview. userinteractionenabled = YES;
  3. self. Abutton = [[UIButton Alloc]initwithframe:cgrectmake (98, 350, 12  5, 25)];
  4. self. Abutton. backgroundcolor = [Uicolor bluecolor];
  5. [self. Abutton addTarget: The self action:@selector (pickerimage:) forcontrolevents: (  UIControlEventTouchUpInside)];
  6. [self. Abutton settitle:@ "Select Image" forstate: (UIControlStateNormal)];
  7. [self. View addsubview:selfAbutton];
  8. [self. View addsubview:selfaimageview];
  9. [self. Abutton release];
  10. [self. Aimageview release];
  11. [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
    1. -(void) addtapgestureonimageview{
    2. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initwithtarget:self action:@  Selector (pickerimage:)];
    3. [self. Aimageview Addgesturerecognizer:tap];
    4. [Tap release];
    5. }

3. Implement the method in the Pat action

[OBJC]View Plaincopy
  1. -(void) Pickerimage: (UIButton *) button{
  2. //Add Actionsheet control, Prompt for options box, bring up camera or take picture
  3. ///First parameter: is the title of the behavior list is generally empty
  4. ///second parameter: Follow the agent
  5. ///Third parameter: Cancels the text displayed on this action button
  6. ///Fourth parameter: destructive destructive, devastating self-understanding anyway, I wrote about taking pictures, doing the action.
  7. ///Fifth parameter: Select a picture from the album
  8. Uiactionsheet *actionsheet = [[Uiactionsheet alloc]initwithtitle: Nil delegate: Self Cancelbuttontitle:@ "Cancel" Destructivebuttontitle:@ "Take photo" Otherbuttontitles:@ "select Picture from album", Nil  Nil];
  9. //Display the Actionsheet object in the current interface
  10. [Actionsheet Showinview:self. view];
  11. [Actionsheet release];
  12. }

4. Implementing methods in the Action Proxy protocol

[OBJC]View Plaincopy
  1. -(void) Actionsheet: (uiactionsheet *) Actionsheet clickedbuttonatindex: (nsinteger) buttonindex{
  2. switch (buttonindex) {
  3. Case 0:
  4. //Call system camera, take photo
  5. [self Pickerpicturefromcamera];
  6. Break ;
  7. case 1:
  8. [self pickerpicturefromphotosalbum];
  9. Default:
  10. Break ;
  11. }
  12. }

4.1 Getting pictures from the camera

[OBJC]View Plaincopy
  1. -(void) pickerpicturefromcamera{
  2. //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
  3. //Determine if the front-facing camera is available
  4. if ([Uiimagepickercontroller Iscameradeviceavailable:uiimagepickercontrollercameradevicefront]) {
  5. NSLog (@ "front-facing camera");
  6. self. imagepc = [[Uiimagepickercontroller alloc]init];
  7. self. Imagepc. cameradevice = Uiimagepickercontrollercameradevicefront;
  8. [self. IMAGEPC release];
  9. //Determine if the rear-facing camera is available
  10. }Else if ([Uiimagepickercontroller iscameradeviceavailable:uiimagepickercontrollercameradevicerear]) {
  11. NSLog (@ "Rear-facing camera");
  12. self. imagepc = [[Uiimagepickercontroller alloc]init];
  13. self. Imagepc. cameradevice = uiimagepickercontrollercameradevicerear;
  14. [self. IMAGEPC release];
  15. //If neither of them is possible, take photos from the mobile album
  16. }else{
  17. Uialertview *alertview = [[Uialertview Alloc]initwithtitle:@ ' call camera failed ' message:@ ' Please select photos from your phone album '  delegate:self cancelbuttontitle:@ "OK" otherbuttontitles: nil, nil nil];
  18. [Alertview show];
  19. [Alertview release];
  20. }
  21. //Initialize Picture Controller object
  22. Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc]init];
  23. //sourcetype Resource Styles
  24. //Set Picture selector to select a picture's style
  25. Imagepicker. sourcetype = uiimagepickercontrollersourcetypephotolibrary;
  26. //Set whether the picture allows editing
  27. Imagepicker. allowsediting = YES;
  28. //Set Picture Selector proxy object for this view controller
  29. Imagepicker. Delegate = self ;
  30. //Push the camera out to modal
  31. [self presentviewcontroller:imagepicker animated:YES completion: nil];
  32. //Release
  33. [Imagepicker release];
  34. }

4.2 Getting pictures from your phone's picture library

[OBJC]View Plaincopy
  1. -(void) pickerpicturefromphotosalbum{
  2. //Initialize Picture Controller object
  3. Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc]init];
  4. //sourcetype Resource Styles
  5. //Set Picture selector to select a picture's style
  6. Imagepicker. sourcetype = uiimagepickercontrollersourcetypephotolibrary;
  7. //Set whether the picture allows editing
  8. Imagepicker. allowsediting = YES;
  9. //Set Picture Selector proxy object for this view controller
  10. Imagepicker. Delegate = self ;
  11. //The selector controller is introduced to the modal
  12. [self presentviewcontroller:imagepicker animated:YES completion: nil];
  13. //Release
  14. [Imagepicker release];
  15. }

5. How to save selected photos to the details page

[OBJC]View Plaincopy
    1. -(void) Imagepickercontroller: (uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: ( nsdictionary *) info{
    2. //Use a photo album as a dictionary to remove photos from
    3. self. Aimageview. image = [info objectforkey:uiimagepickercontrollereditedimage];
    4. //Frame the marquee back
    5. [self dismissviewcontrolleranimated:YES completion: nil];
    6. }

Read albums in iOS, call system camera technology share

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.