2015-01-21 4 views
1

Я делаю приложение, которое на определенной части приложения позволяет сделать снимок с камеры или выбрать из альбома, и это изображение будет показано на ViewController. Это довольно просто, но, что мне нужно, что я не могу это сделать: -I не хочу, чтобы камера открывалась во весь экран, я хочу, чтобы она открывала на ViewController, что пользователь находится в том же месте, где и будет изображение, внутри круга. Мне удалось сделать камеру не открытой во весь экран, но я не могу поместить этот вид в круг и обрезать фотографию в соответствии с кругом.Пользовательская камера на UIView, а не во весь экран

Вот мой код, он может работать, если вы создать стандартный проект, и я думаю, что поможет понять, что происходит:

ПЕРЕМЕННЫХ

@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]; 
} 
+0

Итак, какие результаты ** являются ** Вы получаете в данный момент? – mbm29414

+0

Я могу сделать снимок/выбрать из альбома и показать его на своем экране (без его редактирования в соответствии с кругом/обрезкой). –

+0

Обновлен код. Теперь изображение в порядке, но оно немного вверх. –

ответ

1

С помощью приведенного ниже кода я получаю следующие результаты:

  1. Единый вид с темно-синим фоном.
  2. 2 зеленых круга (кнопки)
  3. Нажатие на левый круг помещает в него «зрение» камеры.
  4. Нажатие на кнопку «взять» (в нижней части круга слева) отключает метод «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 
+0

Мне нужно показать «зрение» камеры по кругу, а не «прямоугольник». –

+0

Также, сделав снимок, фотография, которую пользователь взял (как внутри круга на видении камеры), заменит сам «зрение» камеры. –

+0

Вы скопировали мой код? Я вижу «зрение» камеры в круге, и нажатие кнопки «взять» (внизу «прицела» камеры) заменяет этот круг изображением, которое было сделано. – mbm29414

0

Решено вопрос о «меньшего изображения» на PhotoView после съемки, обрезав его как квадрат и только после того, как установите образ PhotoView.

Решение вопроса «изображение вверх» немного изменилось после установки изображения на PhotoView путем «установки» кадра picker.view на x: 0, y: -50, PhotoView.width, PhotoView.height.

Вот fullcode:

@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 
Смежные вопросы