I have a simple UIImagePickerController which will use the camera to take a picture, but there are a couple things I would like it to do:
Have a custom camera UI
Take a full-screen instead of 480x640 (if on 4inch phone)
Here is my code for showing the UIImagePickerController:
- (IBAction)pick:(id)sender {
NSLog(#"abc");
picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
}
and here is for when the image gets taken:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
}
How do I do this?
Thanks
For the first bullet point I suppose that you could use the cameraOverlayView property of an UIImagePickerController object to add your custom UI over the picker default interface:
- (IBAction)pick:(id)sender {
NSLog(#"abc");
picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
// Add here your custom UI here
[picker setCameraOverlayView:self.customCameraOverlayView];
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
}
EDIT
I've tested the UIImagePickerController and it returns the image with it's full dimensions (PixelXDimension and PixelYDimension):
UIImagePickerControllerMediaMetadata = {
DPIHeight = 72;
DPIWidth = 72;
Orientation = 6;
"{Exif}" = {
ApertureValue = "2.526068811667588";
BrightnessValue = "-0.5779073354035674";
ColorSpace = 1;
DateTimeDigitized = "2013:04:07 22:30:03";
DateTimeOriginal = "2013:04:07 22:30:03";
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.05882352941176471";
FNumber = "2.4";
Flash = 24;
FocalLenIn35mmFilm = 35;
FocalLength = "4.28";
ISOSpeedRatings = (
800
);
MeteringMode = 3;
PixelXDimension = 3264;
PixelYDimension = 2448;
SceneType = 1;
SensingMethod = 2;
ShutterSpeedValue = "4.058893689053568";
SubjectArea = (
1874,
1478,
610,
612
);
WhiteBalance = 0;
};
"{TIFF}" = {
DateTime = "2013:04:07 22:30:03";
Make = Apple;
Model = "iPhone 4S";
Software = "6.1.3";
XResolution = 72;
YResolution = 72;
};
};
EDIT
Also you could set to your image view the content mode to resize and fit the container view:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setImage:image];
}
and after that to fit the imageView as you need in you view controller taking in consideration the device screen (4inch or not) using auto layout or auto sizing.
Related
i am trying to overlaying an image with another image. I have tried this But, its not exactly gives the output what i am expecting. Let me explain it clearly through below image,
Above is my ViewController where i am showing the Camera for taking the picture (Backgorund Image) using UIImagePickerController There is an Overlay Image i have placed. My overlay image can be rotate and zoom it :
if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]) {
cameraPicker = [[UIImagePickerController alloc] init];
cameraPicker.delegate = self;
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
[mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
cameraPicker.mediaTypes = mediaTypes;
cameraPicker.showsCameraControls = NO;
cameraPicker.navigationBarHidden = YES;
cameraPicker.toolbarHidden = YES;
cameraPicker.delegate = self;
cameraPicker.allowsEditing = NO;
[self presentViewController:cameraPicker animated:YES completion:^{
overLayCustomView.hidden = NO;
[cameraPicker.view addSubview:overLayCustomView];
}];
}
Once, the picture has been taken, i have overlaid the image with captured image through below code :
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [self scaleAndRotateImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
UIGraphicsBeginImageContextWithOptions(image.size, FALSE, 0.0);
[image drawInRect:CGRectMake( 0, 0, image.size.width, image.size.height)];
[renderedImage.image drawInRect:CGRectMake( renderedImage.frame.origin.x, renderedImage.frame.origin.y, renderedImage.frame.size.width, renderedImage.frame.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
UIGraphicsEndImageContext();
[picker dismissViewControllerAnimated:YES completion:^{
[self performSegueWithIdentifier:#"previewPage" sender:newImage];
}];
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
// Code here to support video if enabled
}
Code for scaleAndRotateImage
When, i tried these result for the image processing working weird. It gives below issues,
Captured image (Background Image) clarity has been lost.
Overlay image not placed exact point of the frame.
Frame of the overlay image changing and if its rotated not captured as rotated one.
Any, idea about my brief explanation with my code?
Update
Yeah, finally, i have achieved this and using AVCaptureSession to capture a Picture instead of using UIImagePickerController which would be an useful one. Now, the only thing is, My overlay image can be rotated though below one,
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotate:)];
[rotationRecognizer setDelegate:self];
[self.overLayImage addGestureRecognizer:rotationRecognizer];
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}
I am not able to overlay the rotated the overlay image into another one. Not able to get the rotated image? How can i achieve this?
My application receive images from a UIImagePickerController and when the image comes from a 5S I can properly load the image in quick view in debug, however if the image comes from a 4S I cannot view it and my application does not properly work.
Here is my code for the UIImagePickerController
- (IBAction)scanButton:(UIButton *)sender
{
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
_imagePicker.sourceType = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
if (_imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//_imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
_imagePicker.showsCameraControls = NO;
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
_imagePicker.cameraViewTransform = translate;
CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
_imagePicker.cameraViewTransform = scale;
CGRect screenRect = [[UIScreen mainScreen]bounds];
_overlay = [[CameraOverlay alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];
[_overlay.shootButton addTarget:self action:#selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
[_overlay.cancelButton addTarget:self action:#selector(cancelPhoto) forControlEvents:UIControlEventTouchUpInside];
[_overlay.flashButton addTarget:self action:#selector(flashButton) forControlEvents:UIControlEventTouchUpInside];
_imagePicker.cameraOverlayView = _overlay;
}
_imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
_imagePicker.allowsEditing = NO;
[self presentViewController:_imagePicker animated:YES completion:NULL];
}
And my didFinish method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
_image = info[UIImagePickerControllerOriginalImage];
_pickerDismissed = YES;
[self dismissViewControllerAnimated:YES completion:nil];
}
When I view _image from a 5S I can load the image, but from a 4S I cannot.
Does anyone have any idea why this might be happening?
Sounds weird, try a different iPhone 4S
I am making an app for iPhone and want to give users the ability to multiselect images from their photo-library. I already have a working code for user to select four images at a time.
But I can't select 1 or 2 or 3 images at a time. I want to select the 1 or 2 or 3 images at a time.
This is my code. I have struggled for 3 days without finding a solution. Please help me anybody.
Thanks in advance.............
-(void)choosePhotoFromExistingImages
{
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
elcPicker.maximumImagesCount = 4;
elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage
elcPicker.imagePickerDelegate = self;
[self presentViewController:elcPicker animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
}
- (void)displayPickerForGroup:(ALAssetsGroup *)group
{
ELCAssetTablePicker *tablePicker = [[ELCAssetTablePicker alloc] initWithStyle:UITableViewStylePlain];
tablePicker.singleSelection = YES;
tablePicker.immediateReturn = YES;
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:tablePicker];
elcPicker.maximumImagesCount = 0;
elcPicker.imagePickerDelegate = self;
elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage
tablePicker.parent = elcPicker;
// Move me
tablePicker.assetGroup = group;
[tablePicker.assetGroup setAssetsFilter:[ALAssetsFilter allAssets]];
[self presentViewController:elcPicker animated:YES completion:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return YES;
}
else
{
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
#pragma mark ELCImagePickerControllerDelegate Methods
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info)
{
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[self.view addSubview:imageview ];
self.chosenImages = images;
}
UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(10, 240, 40, 40)];
image1.image=[images objectAtIndex:0];
[self.view addSubview:image1];
UIImageView *image2=[[UIImageView alloc]initWithFrame:CGRectMake(60, 240, 40, 40)];
image2.image=[images objectAtIndex:1];
[self.view addSubview:image2];
UIImageView *image3=[[UIImageView alloc]initWithFrame:CGRectMake(120, 240, 40, 40)];
image3.image=[images objectAtIndex:2];
[self.view addSubview:image3];
UIImageView *image4=[[UIImageView alloc]initWithFrame:CGRectMake(180, 240, 40, 40)];
image4.image=[images objectAtIndex:3];
[self.view addSubview:image4];
}
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Here is the link for the Tutorial. Its works fine in my project.Try this
Hope its useful for you.
Check your code in - (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
For example, you create array with length of 2, but in code below you try to get image3.image=[images objectAtIndex:2]; and image4.image=[images objectAtIndex:3];. It's not a surprise that your code throws exception.
Good luck!
I am making an app for iPhone and want to give users the ability to multi-select images from their photo-library. I already have a working code for user to select four images at a time.But i cant select 2 or 3 images at a time.I want to select the 2 or 3 images at a time.I am trying to the following code.please help me anybody.
- (id)initImagePicker
{
ELCAlbumPickerController *albumPicker = [[ELCAlbumPickerController alloc] initWithStyle:UITableViewStylePlain];
self = [super initWithRootViewController:albumPicker];
if (self)
{
self.maximumImagesCount = 4;
[albumPicker setParent:self];
}
else if (self)
{
self.minimumImageCount=1;
[albumPicker setParent:self];
}
return self;
}
- (id)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self)
self.maximumImagesCount = 4;
else if (self)
self.minimumImageCount=1;
return self;
}
-(void)choosePhotoFromExistingImages
{
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
elcPicker.maximumImagesCount = 4;
elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage
elcPicker.imagePickerDelegate = self;
[self presentViewController:elcPicker animated:YES completion:nil];
}
You should try MWPhotoBrowser Control by Michael Waterfall
He has provided single as well as multiple photo selection
Works on iOS 5.1.1+.
All strings are localisable so they can be used in apps that support multiple languages.
In one of my project i've done this thing without ELCImagePickerController. And successfully working that code too. In that project I've added a done button on the navigation bar of image picker controller. Whenever you select a image that image you can grab with in the method called
-(void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
Whenever you are finished with picking you images then you press that done button.with in that method dismiss the view controller.
Using this process you can select and fetch as much as image you want to have.
And my app is working successfully.
Cheers....!!!!!!!
Add done button on the navigation bar of uiimagepickercontroller
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if (!doneButton)
{
doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self action:#selector(saveImagesDone:)];
}
viewController.navigationItem.rightBarButtonItem = doneButton;
}
First display the image picker using UIImagePickerController
Grab your images
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Get the data for the image
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile2 = [self createName:documentsDirectory];
// and then we write it out
[imageData writeToFile:fullPathToFile2 atomically:NO];
}
else
{
// Show Alert
}
}
Close The image picker view controller
-(IBAction)saveImagesDone:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
[self loadimages]; // Load your Images
}
every time you click over an image ,
-(void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)
info this method will grab the image for you.
Don't forgot to upvote... Cheers....!!!!!
you need to implement these methods
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info;
it will return you array of your image selected.(the controller sends back an array of similarly structured dictionaries)
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker;
Try this:
-(id)initImagePicker{
ELCAlbumPickerController *albumPicker = [[ELCAlbumPickerController alloc] initWithStyle:UITableViewStylePlain];
self = [super initWithRootViewController:albumPicker];
if (self) {
self.maximumImagesCount = 4;
self.returnsImage = YES;
self.returnsOriginalImage = YES;
[albumPicker setParent:self];
self.mediaTypes = #[(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie];
}
return self;
}
-(id)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self) {
self.maximumImagesCount = 4;
self.returnsImage = YES;
}
return self;
}
-(void)choosePhotoFromExistingImages
{
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
elcPicker.maximumImagesCount = 4;
elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage
elcPicker.imagePickerDelegate = self;
[self presentViewController:elcPicker animated:YES completion:nil];
}
Bellow is my didFinishPickingMediaWithInfo method and I know that I have a lot of unnecessary code, retains, releases, etc... But this was the way I could get it to work and I'm afraid of breaking it on trying to optimize it.
Can anyone help me on taking off the unnecessary stuff and have this method clean?
Also, I have a view controller that will load up 4 photos (Camera Roll or Camera) and after the 2nd or 3rd photo selection at the picker, the app crashes or at least, I got one of these alerts below. What I'm doing wrong? THanks!
-2012-06-23 10:54:07.399 LookdoDia[6525:707] Received memory warning.
-wait_fences: failed to receive reply: 10004003
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissModalViewControllerAnimated:YES];
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
if(!img) img = [info objectForKey:UIImagePickerControllerOriginalImage];
uiimvImagem.image = [img retain];
uiimvImagem.alpha = 0;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:img];
self.uiimvImagem = [tempImageView retain];
[tempImageView release];
CGRect frame = uiimvImagem.frame;
frame.size.width = 359;
frame.size.height = img.size.height / (img.size.width / 359);
uiimvImagem.frame = frame;
uisv1.showsVerticalScrollIndicator = NO;
uisv1.showsHorizontalScrollIndicator = NO;
uisv1.contentSize = CGSizeMake(uiimvImagem.frame.size.width, uiimvImagem.frame.size.height);
uisv1.maximumZoomScale = 3.0;
uisv1.minimumZoomScale = 0.8;
uisv1.clipsToBounds = YES;
uisv1.delegate = self;
[uisv1 addSubview:uiimvImagem];
[img release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
if(!img) img = [info objectForKey:UIImagePickerControllerOriginalImage];
uiimvImagem.image = img; //why bother?
uiimvImagem.alpha = 0; //why bother?
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:img];
self.uiimvImagem = tempImageView; //Make sure your self.uiimvImagem is a retained property! Also, don't forget to nil it out in your dealloc method
[tempImageView release];
CGRect frame = uiimvImagem.frame;
frame.size.width = 359;
frame.size.height = img.size.height / (img.size.width / 359);
uiimvImagem.frame = frame;
uisv1.showsVerticalScrollIndicator = NO;
uisv1.showsHorizontalScrollIndicator = NO;
uisv1.contentSize = CGSizeMake(uiimvImagem.frame.size.width, uiimvImagem.frame.size.height);
uisv1.maximumZoomScale = 3.0;
uisv1.minimumZoomScale = 0.8;
uisv1.clipsToBounds = YES;
uisv1.delegate = self;
[uisv1 addSubview:uiimvImagem];
[self dismissModalViewControllerAnimated:YES];
}
This should be a bit better - though I'm not sure about how the rest is hooked up (i.e. property declarations and I'm not sure where uisv1 is allocated.) There's still something that doesn't look right - you are assigning an image to an image view but then running the whole thing over with a newly allocated UIImageView.