I'm doing an application that on a specific part of the application, the user will be able to take a picture from the camera or select from the Album and this image will be shown on the ViewController.
This is kind of easy, but, what I need, that I'm not being able to do is:
-I don't want that the Camera open full screen, I want it to 'open' on the ViewController that the user is, in the exactly same place as where the image will be, inside a circle.
I was able to make the Camera not open full screen, but I'm not being able to put this view inside the circle view and crop the photo according to the circle.
Here is my code, it might work if you create a standard project and I think that will help to understand what's going on:
VARIABLES
#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
CODE
#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];
}
With the code below, I get the following results:
A single view with a dark blue background.
2 green circles (the buttons)
Tapping on the left circle puts the camera's "sight" inside of it.
Tapping on the "take" button (at the bottom of the circle on the left) fires off the "takePhotoAction" method.
[Code]
// 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
Solved the issue of the "smaller image" on the PhotoView after taking a picture by cropping it as a square and just after, set the image of the PhotoView.
Solved the issue of the image "going up" a little bit after setting the image on PhotoView by 'setting' the picker.view frame on x: 0, y:-50, PhotoView.width, PhotoView.height.
Here is the 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
When I take a picture with the phone in Portrait mode, using a UIImagePickerController, the UIImage item that is returned has the top of the image facing left and an imageOrientation property of UIImageOrientationRight , // 90 deg CCW, which is as expected (I think).
When I share the image via a UIActivityViewController, all activities seem to behave correctly except for the Copy activity. When I paste the image into another application (for instance, Messages), the image seems to be ignoring the imageOrientation property.
Has anyone else seen this / found a workaround?
Updated with complete code: (project contains storyboard with a single button mapped to self.takeImageButton and - (IBAction)takeImageAndShare:(id)sender;)
#import "ViewController.h"
#interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIButton *takeImageButton;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)takeImageAndShare:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.popoverPresentationController.sourceView = self.takeImageButton;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
NSLog(#"Image Orientation: %li",chosenImage.imageOrientation);
// Now share the selected image
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[chosenImage] applicationActivities:nil];
activityViewController.modalPresentationStyle = UIModalPresentationPopover;
activityViewController.popoverPresentationController.sourceView = self.takeImageButton;
[self presentViewController:activityViewController animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#end
If you run this code, take a photo with the phone in Portrait (.imageOrientation is 3 = UIImageOrientationRight) and select share activities like Messages, Save Image, etc., the image orientation is correct. If you select the copy activity, the image that gets pasted elsewhere is rotated (top of the image is to the left).
Submitted as Radar 19456881. Complete project is available here.
By Default capture image orientation behavior is UIImageOrientationRight, sp you have to set capture image orientation is
[UIDevice currentDevice].orientation
Based on this post, I decided to create my own copy activity:
//
// PhotoActivityCopy.m
//
// Created by Jeff Vautin on 1/13/15.
// Copyright (c) 2015 Jeff Vautin. All rights reserved.
//
#import "PhotoActivityCopy.h"
#import "ImageKit.h"
#import MobileCoreServices;
#interface PhotoActivityCopy ()
#property (strong,nonatomic) NSData *imageJPEGData;
#end
#implementation PhotoActivityCopy
NSString *PhotoActivityCopyActivityType = #"com.me.uiactivitytype.copy";
NSString *PhotoActivityCopyActivityTitle = #"Copy";
+ (UIActivityCategory)activityCategory
{
return UIActivityCategoryAction;
}
- (NSString *)activityType
{
return PhotoActivityCopyActivityType;
}
- (NSString *)activityTitle
{
return PhotoActivityCopyActivityTitle;
}
- (UIImage *)activityImage
{
CGSize imageSize;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
imageSize = CGSizeMake(76.0, 76.0);
} else {
imageSize = CGSizeMake(60.0, 60.0);
}
return [ImageKit imageOfIconCopyWithExportSize:imageSize];
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
BOOL canPerform = NO;
if ([activityItems count] > 1) {
canPerform = NO;
} else if ([[activityItems firstObject] isKindOfClass:[UIImage class]]) {
canPerform = YES;
}
return canPerform;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
self.imageJPEGData = UIImageJPEGRepresentation(activityItems[0], 1.0);
}
- (void)performActivity
{
[[UIPasteboard generalPasteboard] setData:self.imageJPEGData forPasteboardType:(id)kUTTypeJPEG];
[self activityDidFinish:YES];
}
#end
And then I excluded the native copy activity:
PhotoActivityCopy *copyActivity = [[PhotoActivityCopy alloc] init];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[chosenImage] applicationActivities:#[copyActivity]];
activityViewController.excludedActivityTypes = #[UIActivityTypeCopyToPasteboard];
I'm attempting to write an application that shows the camera feed, and you can take an image from it. I've successfully set up a UIButton and a linked action method (IBAction) but I'm having trouble making it trigger the UI to save the picture. I have it all set up so when everything goes correctly it should inform the user, but instead it crashes. Any insight would be greatly appreciated.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)applicationDidBecomeActive
{
//Start the camera up
UIImagePickerController *imageViewPickerController = [[UIImagePickerController alloc] init];
//Hide default UI elements
imageViewPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imageViewPickerController.showsCameraControls = NO;
imageViewPickerController.navigationBarHidden = YES;
imageViewPickerController.toolbarHidden = YES;
//Start the button overlay
UIView *btnView = [[UIView alloc] initWithFrame:imageViewPickerController.view.bounds];
btnView.opaque = NO;
btnView.clipsToBounds = YES;
//Start up the button
UIButton *snapButton = [[UIButton alloc] initWithFrame:CGRectMake(-6, 504, 65, 65)];
snapButton.alpha = 0.5;
snapButton.transform = CGAffineTransformMakeRotation(DEGREES_RADIANS(90));
[snapButton addTarget:snapButton action:#selector(takePhoto:) forControlEvents:UIControlEventTouchUpInside];
//Set the button's picture
UIImage *snapButtonImage = [UIImage imageNamed:#"Button.png"];
[snapButton setImage:snapButtonImage forState:UIControlStateNormal];
//Add button to the overlay
[btnView addSubview:snapButton];
//Overlay button view
imageViewPickerController.cameraOverlayView = btnView;
//Fix for iPhone 5 and make fullscreen
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, -55.0);
CGAffineTransform scale = CGAffineTransformMakeScale(1.333333, 1.333333);
CGAffineTransform rotate = CGAffineTransformMakeRotation(DEGREES_RADIANS(180));
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
imageViewPickerController.cameraViewTransform = transform;
//Let's present it all
[self presentViewController:imageViewPickerController
animated:NO
completion:NULL];
}
- (IBAction)takePhoto:(id)sender {
[UIImagePickerController takePicture];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
EDIT:
Revised code to resolve exception.
- (void)applicationDidBecomeActive
{
//Start the camera up
_imageViewPickerController = [[UIImagePickerController alloc] init];
//Hide default UI elements
_imageViewPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
_imageViewPickerController.showsCameraControls = NO;
_imageViewPickerController.navigationBarHidden = YES;
_imageViewPickerController.toolbarHidden = YES;
//Start the button overlay
UIView *btnView = [[UIView alloc] initWithFrame:_imageViewPickerController.view.bounds];
btnView.opaque = NO;
btnView.clipsToBounds = YES;
//Start up the button
UIButton *snapButton = [[UIButton alloc] initWithFrame:CGRectMake(-6, 504, 65, 65)];
snapButton.alpha = 0.5;
snapButton.transform = CGAffineTransformMakeRotation(DEGREES_RADIANS(90));
[snapButton addTarget:snapButton action:#selector(snapThat) forControlEvents:UIControlEventTouchUpInside];
//Set the button's picture
UIImage *snapButtonImage = [UIImage imageNamed:#"Button.png"];
[snapButton setImage:snapButtonImage forState:UIControlStateNormal];
//Add button to the overlay
[btnView addSubview:snapButton];
//Overlay button view
_imageViewPickerController.cameraOverlayView = btnView;
//Fix for iPhone 5 and make fullscreen
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, -55.0);
CGAffineTransform scale = CGAffineTransformMakeScale(1.333333, 1.333333);
CGAffineTransform rotate = CGAffineTransformMakeRotation(DEGREES_RADIANS(180));
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
_imageViewPickerController.cameraViewTransform = transform;
//Let's present it all
[self presentViewController:_imageViewPickerController
animated:NO
completion:NULL];
}
- (void)snapThat {
[_imageViewPickerController takePicture];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
EDIT 2: Including headers
#interface TurnikitViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
#property (strong, nonatomic) IBOutlet UIView *imageView;
#property (strong, nonatomic) UIImagePickerController *imageViewPickerController;
#end
You should have this as property:
#property (strong, nonatomic) UIImagePickerController *imageViewPickerController;
then you init that as you are doing:
_imageViewPickerController = [[UIImagePickerController alloc] init];
and when you save:
[_imageViewPickerController takePicture];
Instead now, you are not calling the method on the instance, but on the class..that is an error:
[UIImagePickerController takePicture];
And also, you are adding as target of your button, itself..instead the target is self:
[snapButton addTarget:self action:#selector(snapThat) forControlEvents:UIControlEventTouchUpInside];
I want my whole project to run in portrait mode only and only the view that's for viewing the photo's can turn on landscape same as if using the facebook (we view photo's they turn landscape also but other view's remains in portrait) i want to know that how it's done as i have a table view that has images that i get from the DB from the server and every particular data contains different number's of photo's so now i want that after the user select's the photo's they should open up fully and can be viewed in landscape and in portrait as well i did tried
I am working with ios6
- (BOOL)shouldAutorotate
{
return NO;
}
implementing in all the view's so that they can remain in portrait but still they turn in landscape how should i restrict it and please can any one tell me that in one of my class i have images in table view(loaded from the DB) and i have another class that open's up the selected photo from the table
My PhotoView.m class
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = [photos objectAtIndex:indexPath.row];
NSLog(#" *** url is %#",[dic objectForKey:#"url"]);
[self.delegate showPhotoAtUrl:[dic objectForKey:#"url"]];
}
My PhotoViewController.h class
#import <UIKit/UIKit.h>
#interface PhotoViewController : UIViewController <UIScrollViewDelegate>
{
NSString *url;
UIButton *doneButton;
}
#property (nonatomic, retain) UIImageView *imageView;
- (id) initWithPhotoUrl:(NSString *)photoUrl;
#end
and PhotoViewController.m class
#import "PhotoViewController.h"
#interface PhotoViewController ()
#end
#implementation PhotoViewController
#synthesize imageView;
- (id) initWithPhotoUrl:(NSString *)photoUrl
{
self = [super init];
if(self) {
url = [photoUrl retain];
}
return self;
}
- (void)dealloc
{
[url release];
self.imageView = nil;
[doneButton release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.tag = 1;
spinner.center = self.view.center;
[spinner startAnimating];
[self.view addSubview:spinner];
[spinner release];
[self performSelectorInBackground:#selector(loadPhotoData) withObject:nil];
doneButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[doneButton addTarget:self action:#selector(doneTouched) forControlEvents:UIControlEventTouchUpInside];
[doneButton setTitle:#"done" forState:UIControlStateNormal];
[doneButton setBackgroundColor:[UIColor clearColor]];
doneButton.frame = CGRectMake(2, 2, 60, 30);
[self.view addSubview:doneButton];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.title = #"";
self.navigationController.navigationBarHidden = YES;
}
- (void) doneTouched
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void) loadComplete:(NSData *)imageData
{
if(!imageData) return;
UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)[self.view viewWithTag:1];
if(spinner) {
[spinner stopAnimating];
[spinner removeFromSuperview];
}
UIImage *img = [UIImage imageWithData:imageData];
float scale = MIN(self.view.frame.size.width/img.size.width, self.view.frame.size.height/img.size.height);
self.imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width*scale, self.imageView.image.size.height*scale);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.imageView.center = scrollView.center;
scrollView.delegate = self;
scrollView.contentSize = self.imageView.frame.size;
scrollView.minimumZoomScale = 1;
scrollView.maximumZoomScale = 2;
[scrollView addSubview:self.imageView];
[self.view addSubview:scrollView];
[scrollView release];
[self.view bringSubviewToFront:doneButton];
}
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void) loadPhotoData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
[self performSelectorOnMainThread:#selector(loadComplete:) withObject:imageData waitUntilDone:NO];
[pool drain];
}
#end
and in the main class i have have this method that calls the PhotoViewController to load the selected photo
- (void) showPhotoAtUrl:(NSString *)url
{
PhotoViewController *vc = [[PhotoViewController alloc] initWithPhotoUrl:url];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
Now my problem is how should i get the images after getting selected from the table to be open up in the same type of the view that open's up in FB app (open's the selected photo in portrait/landscape compatible view) i am only able to get one photo in my PhotoViewController class can any one tell me how can i add all the photo's to the PhotoViewController class so that i get the effect that i want ?... Any code help will be very helpful .. Thanks in Advance for contributing your time
In Short i have two things to do :
I have to restrict my project to only portrait mode leaving only the photo's view to have landscape/portrait compatibility.
I am currently having the photo's in my table view that i want on selection to open up in the same style(landscape/portrait compatibile) as FB or other app's do while viewing the photo's.
These only work on iOS 6.0
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft);
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
The method will return which orientation mode to use first.
Below are supportedInterfaceOrientations:
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown
For iOS > 4.0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Other ViewControllers will have
-(BOOL)shouldAutorotate { return NO; }
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation{
return NO;
}
Essentially I have a UIAlertView that pops up. When a user selects a button it calls itself, and based on the button index it brings up a UIImageView subview. My question is, because the UIImageView takes up a good portion of the screen it lays on top of the UIAlertView, so the cancel button is unseen. I was wondering if I could create actions on the UIImageView so that if it is clicked or touched up inside, the UIAlertView/UIImageView would resign and disappear?
Someone please help, I've been tooling around this for hours.
Here is the code from the button being clicked, and the button index.
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
// exit(0);
}
else
{
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:#"molecule" message:molURL delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];
NSString *MyURL = molURL;
NSString *apURL = [NSString stringWithFormat:#"%#",MyURL];
NSURL * imageURL = [NSURL URLWithString:apURL];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image1 = [UIImage imageWithData:imageData];
//UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:image1];
[successAlert addSubview:imageView];
[successAlert show];
}
}
MyImageView.h
#import <UIKit/UIKit.h>
#interface MyImageView : UIImageView {
}
#end
MyImageView.m
#import "MyImageView.h"
#implementation MyImageView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled=YES;
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[UIView animateWithDuration:2.0f animations:^(void) {
self.alpha=0.0f;
super.hidden =YES;
UIAlertView *alert=(UIAlertView*)self.superview;
alert.alpha=0.0f;
[alert dismissWithClickedButtonIndex:0 animated:YES];
//or
//[alert removeFromSuperview];
}];
}
- (void)dealloc {
[super dealloc];
}
#end
MyViewController.h
#class MyImageView;
MyViewController.m
#import "MyImageView.h"
then while create imageView
MyImageView *specialImageView =[[MyImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 260)];