How to add overlay to image picker in iOS - ios

I am trying to add an overlay to image taken with imagePicker such as a frame, shown during snap taking. I was trying to use the following code:
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = YES;
// Insert the overlay
self.overlay = [[OverlayViewController alloc] initWithNibName:#"Overlay" bundle:nil];
self.overlay.pickerReference = self.picker;
self.picker.cameraOverlayView = self.overlay.view;
self.picker.delegate = self.overlay;
[self presentModalViewController:self.picker animated:NO];
But this helps when changing the image picker skin. How can I add and process an overlay with the snapshot?

If you need to merge a picture taken with another UIImageView that contains an image, here's a useful snippet:
yourFile.h:
UIImage *combinedImage;
yourFile.m:
-(void)savePicture { // call this method from an IBAction button
// Crop a Combined Image from the taken picture
CGRect rect = [_imagePreview bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
// Render both images, the captured one from the camera and the UIImageView that is over that picture
[__myCapturedImage.layer renderInContext:UIGraphicsGetCurrentContext()];
[_overlayImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_myCapturedImage.image = combinedImage;
// Automatically Save the Image into Photo Album (if you want to do so)
UIImageWriteToSavedPhotosAlbum(combinedImage, nil, nil, nil);
}
Hope this helps!
PS: we have made a nice template that actually processes overlays images (which are frames around images) here: http://codecanyon.net/item/picshape-frame-image-editor-full-app-template/7852895?ref=fvimagination
Cheers!

Related

Overlaying an Image into another

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?

how to add another image or animation in iOS camera

Just like Häagen-Dazs' App, I think it just add an animation before the photo iPhone is taking. I have checked UIImagePickerController and it didn't say how to add another picture or animation before the image. I just want to add some interesting things in front of the scene that I'm taking. Could anyone provide any tutorial? Thanks.
add you custom UIView with animation like this into ImagePickerView
_imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePickerCamera.delegate = self;
_imagePickerCamera.showsCameraControls = NO;
_imagePickerCamera.navigationBarHidden = YES;
_imagePickerCamera.toolbarHidden = YES;
_imagePickerCamera.wantsFullScreenLayout = YES;
//create an overlay view instance
OverlayView *overlay = [[OverlayView alloc]
initWithFrame:self.view.bounds];
//set our custom overlay view
_imagePickerCamera.cameraOverlayView = overlay;
overlay.captureButton.frame = CGRectMake(self.view.frame.size.width/2 - 30, self.view.frame.size.height - 60, 60, 60);
[overlay.captureButton addTarget:self action:#selector(captureImageWithButton:) forControlEvents:UIControlEventTouchUpInside];
[self performSelector:#selector(presentCameraView) withObject:nil afterDelay:0.0f];
You can add any overlay view you want using the cameraOverlayView property of the UIImagePickerController class.

iOS UIImagePickerController, set frame

I'm trying to accomplish the following result:
Set the frame of my UIImagePickerController to, lets say 200 x 200,
Set my frame at the bottom-right corner (just like Facetime/Skype does)
and show the front/rear (doesn't matter) camera stream.
Here's my code, for some reason, setFrame is not working!
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.view.frame = CGRectMake(600, 400, 200, 200); // NOT WORKING !!!
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = NO;
self.picker.delegate = delegate;
[self presentViewController:self.picker animated:YES completion:nil];
I've looked at similar SO topics but they all talk about how to
set a view on top of the UIImagePickerController, not my problem at all.
I've tried adding self.picker to a custom UIView sized 200 x 200 but still
no success.
What am i doing wrong here?
Thanks
You need to use the cameraViewTransform property of UIImagePicker to adjust its camera frame. This property accept any CGAffineTransform you made (e.g. scaling, rotating, inverting, translating, etc).
For example, if you want to move the camera view down by 50 points and scale the camera view 1.2x its original size, this is how you'll do it:
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0f, 50.0f);
transform = CGAffineTransformScale(transform, 1.2f, 1.2f);
imagePicker.cameraViewTransform = transform;
Swift 5:
var transform = CGAffineTransform(translationX: 0.0, y: 50.0)
transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
imagePicker.cameraViewTransform = transform
From UIViewController reference link
presentViewController:animated:completion:
Discussion
On iPhone and iPod touch, the presented view is always full screen
If you want to embed image picker, you should manually add its view to one of your other views.
like this
[self.view addSubview:self.imagePickerController.view];
I experimented with the code from my last post, and commented out the final scale transform ((the one which makes it full size) and I ended up with a lovely miniature camera imagePicker floating in the middle of my screen, so it definitely does work! The exact code I used, including the zoom/fade-in transition, is -
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
UIView *controllerView = imagePickerController.view;
controllerView.alpha = 0.0;
controllerView.transform = CGAffineTransformMakeScale(0.5, 0.5);
[[[[UIApplication sharedApplication] delegate] window] addSubview:controllerView];
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
controllerView.alpha = 1.0;
}
completion:nil
];
[imagePickerController release];

UIImagePickerController and a small custom preview

I want to use an UIImagePickerController non-modally, with a small square preview. However, I get a big black bar at the bottom of the preview, which I can't get rid of (see the screenshot). Why is it there, and how can I get rid of it?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
_imagePickerVC = [[UIImagePickerController alloc] init];
_imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePickerVC.mediaTypes = #[ (NSString *)kUTTypeImage ];
_imagePickerVC.wantsFullScreenLayout = NO;
_imagePickerVC.delegate = (id)self;
_imagePickerVC.navigationBarHidden = YES;
_imagePickerVC.allowsEditing = NO;
_imagePickerVC.showsCameraControls = NO;
_imagePickerVC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
CGRect previewFrame = CGRectMake(0, 0, 150, 150);
[_imagePickerVC.view setFrame:previewFrame];
_imagePickerVC.view.autoresizesSubviews = YES;
[self.view addSubview:_imagePickerVC.view];
[_imagePickerVC viewWillAppear:YES];
[_imagePickerVC viewDidAppear:YES];
}
This is a misuse of UIImagePickerController. To make your own image-capture interface, simply use AVFoundation.

UIImagePickerController didFinishPickingMediaWithInfo

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.

Resources