UIView 上的自定义相机,而不是全屏
Custom camera on a UIView, not full screen
我正在做一个应用程序,在应用程序的特定部分,用户将能够从相机或 select 从相册中拍摄照片,并且该图像将显示在 ViewController。
这有点简单,但是,我需要的是,我无法做到的是:
- 我不希望相机全屏打开,我希望它在用户所在的 ViewController 上 'open',与图像所在的位置完全相同,在一个圆圈内。
我可以让相机不全屏打开,但我无法将此视图放入圆形视图中并根据圆形裁剪照片。
这是我的代码,如果您创建一个标准项目,它可能会起作用,我认为这将有助于理解正在发生的事情:
变量
@property (nonatomic, strong) UIImagePickerController * picker;
@property (nonatomic, strong) UIImageView * photoView;
@property (nonatomic, strong) UIView * overlayView;
@property (nonatomic, strong) UIButton * takePhotoButton; //- function
@property (nonatomic, strong) UIButton * selectPhotoButton;
@property (nonatomic, strong) UIButton * takePicture; //- action
代码
@interface CameraViewController ()
{
BOOL isFromAlbum;
}
@end
@implementation CameraViewController
@synthesize photoView;
@synthesize picker;
@synthesize takePicture;
@synthesize selectPhotoButton;
@synthesize takePhotoButton;
@synthesize overlayView;
@synthesize switchCameraButton;
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface
{
[super addBackButton];
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:@"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:@selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
//[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:@"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
//[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = CGRectMake(0, 0, 320, 320);
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
self.photoView.userInteractionEnabled = YES;
}
#pragma mark -
#pragma mark Button's Actions
- (void)takePhotoFunction
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
self.picker.view.frame = self.photoView.bounds;
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 70, 70, 70);
[self.takePicture addTarget:self action:@selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:@"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
self.switchCameraButton = [[UIButton alloc] init];
self.switchCameraButton.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.origin.y, 70, 70);
[self.switchCameraButton addTarget:self action:@selector(switchCamera:) forControlEvents:UIControlEventTouchUpInside];
[self.switchCameraButton setBackgroundImage:[UIImage imageNamed:@"reverse-camera-btn"] forState:UIControlStateNormal];
[self.photoView addSubview:self.switchCameraButton];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender
{
[self.picker takePicture];
}
-(IBAction)switchCamera:(id)sender
{
if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
else if (picker.cameraDevice == UIImagePickerControllerCameraDeviceRear)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO)
{
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage)
{
imageToSave = editedImage;
}
else
{
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.switchCameraButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
CGFloat minimumSide = fminf(imageToSave.size.width, imageToSave.size.height);
CGFloat finalSquareSize = 640.;
//create new drawing context for right size
CGRect rect = CGRectMake(0, 0, finalSquareSize, finalSquareSize);
CGFloat scalingRatio = 640.0/minimumSide;
UIGraphicsBeginImageContext(rect.size);
//draw
[imageToSave drawInRect:CGRectMake((minimumSide - originalImage.size.width)*scalingRatio/2., (minimumSide - originalImage.size.height)*scalingRatio/2., originalImage.size.width*scalingRatio, originalImage.size.height*scalingRatio)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[self.photoView setImage:imageToSave];
[self.photoView setImage:croppedImage];
}
else
{
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker
{
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
使用下面的代码,我得到以下结果:
- 深蓝色背景的单个视图。
- 2 个绿色圆圈(按钮)
- 点击左侧圆圈可将相机 "sight" 放入其中。
- 点击 "take" 按钮(在左侧圆圈底部)会触发 "takePhotoAction" 方法。
[代码]
// ViewController.m
#import "ViewController.h"
@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
BOOL isFromAlbum;
}
@property (nonatomic, strong) UIImagePickerController * picker;
@property (nonatomic, strong) UIImageView * photoView;
@property (nonatomic, strong) UIView * overlayView;
@property (nonatomic, strong) UIButton * takePhotoButton; //- function
@property (nonatomic, strong) UIButton * selectPhotoButton;
@property (nonatomic, strong) UIButton * takePicture; //- action
@end
@implementation ViewController
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface {
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:@"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:@selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:@"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = self.takePhotoButton.frame;
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
[self.photoView setUserInteractionEnabled:YES];
}
#pragma mark -
#pragma mark Actions
- (void)takePhotoFunction {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
self.picker.view.frame = self.photoView.bounds;
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 40, 70, 70);
[self.takePicture addTarget:self action:@selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:@"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender {
[self.picker takePicture];
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
[self.photoView setImage:imageToSave];
} else {
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker {
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
@end
通过将照片裁剪为正方形解决了 PhotoView 上 "smaller image" 的问题,然后设置 PhotoView 的图像。
通过 'setting' picker.view 框架在 x: 0, y:-50, PhotoView.width, PhotoView.height.
完整代码如下:
@interface CameraViewController ()
{
BOOL isFromAlbum;
}
@end
@implementation CameraViewController
@synthesize photoView;
@synthesize picker;
@synthesize takePicture;
@synthesize selectPhotoButton;
@synthesize takePhotoButton;
@synthesize switchCameraButton;
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface
{
[super addBackButton];
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:@"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:@selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
//[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:@"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
//[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = CGRectMake(0, 0, 320, 320);
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
self.photoView.userInteractionEnabled = YES;
}
#pragma mark -
#pragma mark Button's Actions
- (void)takePhotoFunction
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
//self.picker.view.frame = self.photoView.bounds;
self.picker.view.frame = CGRectMake(0, -50, photoView.frame.size.width, photoView.frame.size.height + 50);
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 70, 70, 70);
[self.takePicture addTarget:self action:@selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:@"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
self.switchCameraButton = [[UIButton alloc] init];
self.switchCameraButton.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.origin.y, 70, 70);
[self.switchCameraButton addTarget:self action:@selector(switchCamera:) forControlEvents:UIControlEventTouchUpInside];
[self.switchCameraButton setBackgroundImage:[UIImage imageNamed:@"reverse-camera-btn"] forState:UIControlStateNormal];
[self.photoView addSubview:self.switchCameraButton];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender
{
[self.picker takePicture];
}
-(IBAction)switchCamera:(id)sender
{
if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
else if (picker.cameraDevice == UIImagePickerControllerCameraDeviceRear)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
}
- (UIImage*)rotateUIImage:(UIImage*)sourceImage
{
CGSize size = sourceImage.size;
UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width));
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:UIImageOrientationDownMirrored] drawInRect:CGRectMake(0,0,size.height ,size.width)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO)
{
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage)
{
imageToSave = editedImage;
}
else
{
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.switchCameraButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
CGFloat minimumSide = fminf(imageToSave.size.width, imageToSave.size.height);
CGFloat finalSquareSize = 640.;
//create new drawing context for right size
CGRect rect = CGRectMake(0, 0, finalSquareSize, finalSquareSize);
CGFloat scalingRatio = 640.0/minimumSide;
UIGraphicsBeginImageContext(rect.size);
//draw
[imageToSave drawInRect:CGRectMake((minimumSide - originalImage.size.width)*scalingRatio/2., (minimumSide - originalImage.size.height)*scalingRatio/2., originalImage.size.width*scalingRatio, originalImage.size.height*scalingRatio)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
// {
// [self rotateUIImage:croppedImage];
// }
//[self.photoView setImage:imageToSave];
[self.photoView setImage:croppedImage];
}
else
{
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker
{
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
@end
我正在做一个应用程序,在应用程序的特定部分,用户将能够从相机或 select 从相册中拍摄照片,并且该图像将显示在 ViewController。 这有点简单,但是,我需要的是,我无法做到的是: - 我不希望相机全屏打开,我希望它在用户所在的 ViewController 上 'open',与图像所在的位置完全相同,在一个圆圈内。 我可以让相机不全屏打开,但我无法将此视图放入圆形视图中并根据圆形裁剪照片。
这是我的代码,如果您创建一个标准项目,它可能会起作用,我认为这将有助于理解正在发生的事情:
变量
@property (nonatomic, strong) UIImagePickerController * picker;
@property (nonatomic, strong) UIImageView * photoView;
@property (nonatomic, strong) UIView * overlayView;
@property (nonatomic, strong) UIButton * takePhotoButton; //- function
@property (nonatomic, strong) UIButton * selectPhotoButton;
@property (nonatomic, strong) UIButton * takePicture; //- action
代码
@interface CameraViewController ()
{
BOOL isFromAlbum;
}
@end
@implementation CameraViewController
@synthesize photoView;
@synthesize picker;
@synthesize takePicture;
@synthesize selectPhotoButton;
@synthesize takePhotoButton;
@synthesize overlayView;
@synthesize switchCameraButton;
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface
{
[super addBackButton];
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:@"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:@selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
//[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:@"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
//[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = CGRectMake(0, 0, 320, 320);
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
self.photoView.userInteractionEnabled = YES;
}
#pragma mark -
#pragma mark Button's Actions
- (void)takePhotoFunction
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
self.picker.view.frame = self.photoView.bounds;
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 70, 70, 70);
[self.takePicture addTarget:self action:@selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:@"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
self.switchCameraButton = [[UIButton alloc] init];
self.switchCameraButton.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.origin.y, 70, 70);
[self.switchCameraButton addTarget:self action:@selector(switchCamera:) forControlEvents:UIControlEventTouchUpInside];
[self.switchCameraButton setBackgroundImage:[UIImage imageNamed:@"reverse-camera-btn"] forState:UIControlStateNormal];
[self.photoView addSubview:self.switchCameraButton];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender
{
[self.picker takePicture];
}
-(IBAction)switchCamera:(id)sender
{
if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
else if (picker.cameraDevice == UIImagePickerControllerCameraDeviceRear)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO)
{
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage)
{
imageToSave = editedImage;
}
else
{
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.switchCameraButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
CGFloat minimumSide = fminf(imageToSave.size.width, imageToSave.size.height);
CGFloat finalSquareSize = 640.;
//create new drawing context for right size
CGRect rect = CGRectMake(0, 0, finalSquareSize, finalSquareSize);
CGFloat scalingRatio = 640.0/minimumSide;
UIGraphicsBeginImageContext(rect.size);
//draw
[imageToSave drawInRect:CGRectMake((minimumSide - originalImage.size.width)*scalingRatio/2., (minimumSide - originalImage.size.height)*scalingRatio/2., originalImage.size.width*scalingRatio, originalImage.size.height*scalingRatio)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[self.photoView setImage:imageToSave];
[self.photoView setImage:croppedImage];
}
else
{
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker
{
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
使用下面的代码,我得到以下结果:
- 深蓝色背景的单个视图。
- 2 个绿色圆圈(按钮)
- 点击左侧圆圈可将相机 "sight" 放入其中。
- 点击 "take" 按钮(在左侧圆圈底部)会触发 "takePhotoAction" 方法。
[代码]
// ViewController.m
#import "ViewController.h"
@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
BOOL isFromAlbum;
}
@property (nonatomic, strong) UIImagePickerController * picker;
@property (nonatomic, strong) UIImageView * photoView;
@property (nonatomic, strong) UIView * overlayView;
@property (nonatomic, strong) UIButton * takePhotoButton; //- function
@property (nonatomic, strong) UIButton * selectPhotoButton;
@property (nonatomic, strong) UIButton * takePicture; //- action
@end
@implementation ViewController
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface {
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:@"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:@selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:@"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = self.takePhotoButton.frame;
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
[self.photoView setUserInteractionEnabled:YES];
}
#pragma mark -
#pragma mark Actions
- (void)takePhotoFunction {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
self.picker.view.frame = self.photoView.bounds;
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 40, 70, 70);
[self.takePicture addTarget:self action:@selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:@"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender {
[self.picker takePicture];
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
[self.photoView setImage:imageToSave];
} else {
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker {
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
@end
通过将照片裁剪为正方形解决了 PhotoView 上 "smaller image" 的问题,然后设置 PhotoView 的图像。
通过 'setting' picker.view 框架在 x: 0, y:-50, PhotoView.width, PhotoView.height.
完整代码如下:
@interface CameraViewController ()
{
BOOL isFromAlbum;
}
@end
@implementation CameraViewController
@synthesize photoView;
@synthesize picker;
@synthesize takePicture;
@synthesize selectPhotoButton;
@synthesize takePhotoButton;
@synthesize switchCameraButton;
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface
{
[super addBackButton];
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:@"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:@selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
//[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:@"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
//[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = CGRectMake(0, 0, 320, 320);
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
self.photoView.userInteractionEnabled = YES;
}
#pragma mark -
#pragma mark Button's Actions
- (void)takePhotoFunction
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
//self.picker.view.frame = self.photoView.bounds;
self.picker.view.frame = CGRectMake(0, -50, photoView.frame.size.width, photoView.frame.size.height + 50);
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 70, 70, 70);
[self.takePicture addTarget:self action:@selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:@"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
self.switchCameraButton = [[UIButton alloc] init];
self.switchCameraButton.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.origin.y, 70, 70);
[self.switchCameraButton addTarget:self action:@selector(switchCamera:) forControlEvents:UIControlEventTouchUpInside];
[self.switchCameraButton setBackgroundImage:[UIImage imageNamed:@"reverse-camera-btn"] forState:UIControlStateNormal];
[self.photoView addSubview:self.switchCameraButton];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender
{
[self.picker takePicture];
}
-(IBAction)switchCamera:(id)sender
{
if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
else if (picker.cameraDevice == UIImagePickerControllerCameraDeviceRear)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
}
- (UIImage*)rotateUIImage:(UIImage*)sourceImage
{
CGSize size = sourceImage.size;
UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width));
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:UIImageOrientationDownMirrored] drawInRect:CGRectMake(0,0,size.height ,size.width)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO)
{
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage)
{
imageToSave = editedImage;
}
else
{
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.switchCameraButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
CGFloat minimumSide = fminf(imageToSave.size.width, imageToSave.size.height);
CGFloat finalSquareSize = 640.;
//create new drawing context for right size
CGRect rect = CGRectMake(0, 0, finalSquareSize, finalSquareSize);
CGFloat scalingRatio = 640.0/minimumSide;
UIGraphicsBeginImageContext(rect.size);
//draw
[imageToSave drawInRect:CGRectMake((minimumSide - originalImage.size.width)*scalingRatio/2., (minimumSide - originalImage.size.height)*scalingRatio/2., originalImage.size.width*scalingRatio, originalImage.size.height*scalingRatio)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
// {
// [self rotateUIImage:croppedImage];
// }
//[self.photoView setImage:imageToSave];
[self.photoView setImage:croppedImage];
}
else
{
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker
{
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
@end