UIImagePickerController: how to obtain photos, add the overlay method (CAMERA framing ),

Source: Internet
Author: User

UIImagePickerController: how to obtain photos, add the overlay method (CAMERA framing ),
DEVELOPER. XIAOYAOLI technical notes

You can use UIImagePickerController to call the iPhone camera to obtain photos. It also introduces how to add overlay to customize the preview interface.

 

 

 

UIImagePickerController is a method for obtaining the Camera Photo viewfinder. The simple implementation method is as follows. I added the main notes and note that

<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

12345678910111213141516171819202122232425 -(IBAction) cameraBtn :( id) sender {UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init]; imagePicker. delegate = self; imagePicker. sourceType = UIImagePickerControllerSourceTypeCamera; // the camera or album imagePicker can be obtained. modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker. allowsEditing = NO; // if the picture taken for NO is the original image, for example, the size of the iPhone 4s and 5 should be (2000 +) * (3000 + ), almost pixels. If YES, there will be a square box in the selected area // imagePicker. showsCameraControls = NO; // The camera key is enabled by default, and the camera switch control is enabled, used to customize ovelay when set to NO. // UIImageView * overLayImg = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0,320,640)]; // overLayImg. image = [UIImage imageNamed: @ "overlay.png"]; // imagePicker. cameraOverlayView = overLayImg; // after 3.0, you can directly set cameraOverlayView to overlay // imagePicker. wantsFullScreenLayout = YES; [self presentModalViewController: imagePicker animated: YES];}

 

This code can be used to implement the photo taking function. if you add the commented-out code, you can add overlayview, such as cross-star, bar code scanning box, and so on.

So how can we process the obtained images? Use the following proxy method

-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info

{

UIImage * image = [info objectForKey: @ "UIImagePickerControllerOriginalImage"]; // you can print this info. If you can edit it, obtain editedImage instead of OriginalImage.

 

NSData * data = UIImageJPEGRepresentation (image, 1); // convert to JPEG encoding

UIImage * finalImg = [[UIImage imageWithData: data] fixOrientation];

[Self saveImage: finalImg WithName: @”salesImageBig.jpg "];

[Self dismissModalViewControllerAnimated: YES];

 

}

In this case, you can take photos to obtain photos.

Supplement to OVERLAY

Here, I would like to add a simple overlay principle, but there will be some problems in actual application. First, let's talk about the situation when taking photos from the camera.

 
1 ImagePicker. sourceType = UIImagePickerControllerSourceTypeCamera;

At this time, there is no status bar. After testing (the document is not found), the cropping window in the Edit state is 320*320, and Y is 75. So I can precisely match the overlay position.

If it is obtained by album, you cannot set overlay when creating a UIImagePicker. As the name suggests, cameraOverlayView is prepared for the camera, therefore, you must use the UINavigationController's proxy Method to Determine the level and then add overlay at the current level.

In addition, this is not 75, It is 95, because album has a status bar, just like the first image in the article.

 

 

 

Implementation Method:

 
1234567891011121314151617 -(Void) navigationController :( UINavigationController *) navigationController didShowViewController :( UIViewController *) viewController animated :( BOOL) animated {if ([navigationController. viewControllers count] = 3) {// Add the custom information layer CameraOverLayFrameView * overLayView = [[[NSBundle mainBundle] loadNibNamed: @ "CameraOverLayFrameView" owner: self options: nil] objectAtIndex: 0]; CGRect theOverLayFrame = CGRectMake (30, 95,260,320); [overLayView setFrame: theOverLayFrame]; [viewController. view addSubview: overLayView];}

As for why 3, you can see it after a try. This layer is the third-level view pushed by navigation.

This may seem like a success, but in fact we will find that in the edit mode, this overLayerView blocks the movement and drag gestures, and it is useless to set the userInterface, therefore, you need to implement this method in the Custom overLayView:

1234567 -(BOOL) pointInside :( CGPoint) point withEvent :( UIEvent *) event {return NO ;}

 

This is all done.

 

Related Article

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.