UIImagePickerController: invoke takePicture without shuttering animation - objective-c

I'm invoking UIImagePickerController's method takePicture method to programmatically pictures with the iPhone
However I would like to disable the shuttering animation, and just leave the screen as it is, when the picture is taken (and disabling the sound as well).
UPDATE
I'm now using AVFoundation APIs to get the image, but the content of the camera is not displayed anymore on the screen. Should I initialize a UIImagePickerController just to display the content of the camera on the screen ?
Thanks

Check out this sample: It uses AVCaptureSession and an ImagePicker to capture images from the video.
http://developer.apple.com/library/ios/#samplecode/AVCam/Listings/Classes_AVCamCaptureManager_m.html
Also this one: https://github.com/jj0b/AROverlayExample

UIImagePickerController has some options, but not a lot. This other question has some tacky but probably effective ideas for disabling it.
For complete control, you need to use the AVFoundation APIs. Here is a capture example: https://developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112

I think this useful for 3.0
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"])
{
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage * cameraimage= [self scaleAndRotateImage:selectedImage];
CGRect rect =CGRectMake(0, 0,640,875);
CGImageRef imageRef = CGImageCreateWithImageInRect([selectedImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
self.captureimage.image=cameraimage;
NSLog(#"selectedimage width:%f ht:%f",img.size.width,img.size.height);
}
[picker dismissModalViewControllerAnimated:YES];
}

Related

App crashs in drawViewHierarchyInRect

My app is used large images (5MB) to markup. But app is getting crashed without any error messages.
Below is the code I used
UIGraphicsBeginImageContext(CGSizeMake(_drawingUV.drawingImgView.image.size.width, _drawingUV.drawingImgView.image.size.height));
{
CGContextRef context = UIGraphicsGetCurrentContext();
if ([_drawingUV.drawingImgView respondsToSelector:#selector(drawViewHierarchyInRect:afterScreenUpdates:)])
{
[_drawingUV.drawingImgView drawViewHierarchyInRect:_drawingUV.drawingImgView.bounds afterScreenUpdates:YES];
}
else
{
[_drawingUV.drawingImgView.layer renderInContext:context];
}
screenShot = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
I have tried all possibilities to find a solution last two days. But could not.
This function is used below UI objects.
main view has view and inside that it has UIScrollView and inside that UIScrollView has UIImageView. So I need to get screenshoto this UIImageView. I use these views to scroll and markup the the image view.
Any idea for this crash?
I use this all the time without issue:
- (UIImage *)screenshot {
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContextWithOptions(mainWindow.bounds.size, NO, 1.0);
[mainWindow drawViewHierarchyInRect:mainWindow.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

iOS full screen camera overlay

I am new to objective-c and have been searching for an answer to this question for a while...
I am creating a photo-sharing app and I want the camera to be full-screen. I have used the following code to hide the default camera overlay and create my own, and make the camera full-screen. On the next screen, I want to display the image captured as full-screen again, but the image is getting collapsed sideways.
Setting the camera overlay:
-(void)loadCamera {
self.imagePicker = [[UIImagePickerController alloc]init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.delegate = self;
self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.imagePicker.showsCameraControls = NO;
self.overlayIV = [[WAUCameraOverlayView alloc]initWithFrame:self.imagePicker.cameraOverlayView.frame];
[self.imagePicker.cameraOverlayView addSubview:self.overlayIV];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
self.imagePicker.cameraViewTransform = translate;
CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
self.imagePicker.cameraViewTransform = scale;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
The above code works great and makes the camera full-screen with my custom overlay. Then, this block of code gets executed:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self.overlayIV removeFromSuperview];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
I pass "image" to the next screen and initialize it to the size of the screen, but it gets stretched... any way to save/display the image captured from the full-screen camera in the exact ratio and size?
EDIT: The code I use to display the image is:
UIImageView *capturedImageView =[[UIImageView alloc]initWithFrame:self.frame];
capturedImageView.image = aImage;
capturedImageView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:capturedImageView];
I want the image to be the size of the screen, and the same ratio/aspect as it was captured in the camera

Objective-C: Use the Original Image Size After Capture

I am making an app that uses the camera.
Everything is working well, but when i started to add the captured image on a UIImageView it resizes automatically with respect to the size of UIImageView.
What i want it to do is, use the original size of the captured image.
Here's my code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[insertPhoto1 setImage:image];
[self dismissModalViewControllerAnimated:YES];
}
The problem is with the contentMode of the imageView:
insertPhoto1.contentMode=UIViewContentModeCenter;

Programmatically take screenshots on iOS from anywhere

I am currently using this code to capture the screen:
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(keyWindow.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(keyWindow.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
It works fine from the Springboard but in an opened application, there is no image created. The key window and [UIScreen mainScreen] should work from anywhere or should they not?
Do I have to get more specific and somehow use the specific window that the current app is using?
It may be a silly answer, but are you sure that the view is loaded when you try to capture the key window?
HOpe this helps.
This is for Jailbroken iOS only:
// fwd declare a private function
UIKIT_EXTERN CGImageRef UIGetScreenImage();
// grab screen image from framebuffer.
// this grabs everything: status bar included
CGImageRef ref = UIGetScreenImage();
// create a UIImage out of CoreGraphics object
// at this point img contains your screenshot
UIImage* img = [UIImage imageWithCGImage:ref];
// release temp object
CGImageRelease(ref);

UITableViewCell's imageView fit to 40x40

I use the same big images in a tableView and detailView.
Need to make imageView filled in 40x40 when an imags is showed in tableView, but stretched on a half of a screen. I played with several properties but have no positive result:
[cell.imageView setBounds:CGRectMake(0, 0, 50, 50)];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFill];
I am using SDK 3.0 with build in "Cell Objects in Predefined Styles".
I put Ben's code as an extension in my NS-Extensions file so that I can tell any image to make a thumbnail of itself, as in:
UIImage *bigImage = [UIImage imageNamed:#"yourImage.png"];
UIImage *thumb = [bigImage makeThumbnailOfSize:CGSizeMake(50,50)];
Here is .h file:
#interface UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size;
#end
and then in the NS-Extensions.m file:
#implementation UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
// draw scaled image into thumbnail context
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();
// pop the context
UIGraphicsEndImageContext();
if(newThumbnail == nil)
NSLog(#"could not scale image");
return newThumbnail;
}
#end
I cache a thumbnail version since using large images scaled down on the fly uses too much memory.
Here's my thumbnail code:
- (UIImage *)thumbnailOfSize:(CGSize)size {
if( self.previewThumbnail )
return self.previewThumbnail; // returned cached thumbnail
UIGraphicsBeginImageContext(size);
// draw scaled image into thumbnail context
[self.preview drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();
// pop the context
UIGraphicsEndImageContext();
if(newThumbnail == nil)
NSLog(#"could not scale image");
self.previewThumbnail = newThumbnail;
return self.previewThumbnail;
}
Just make sure you properly clear the cached thumbnail if you change your original image (self.preview in my case).
I have mine wrapped in a UIView and use this code:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
[self addSubview:imageView];
imageView.frame = self.bounds;
(self is the wrapper UIView, with the dimensions I want - I use AsyncImageView).
I thought Ben Lachman's suggestion of generating thumbnails in advance rather than on the fly was smart, so I adapted his code so it could handle a whole array and to make it more portable (no hard-coded property names).
- (NSArray *)arrayOfThumbnailsOfSize:(CGSize)size fromArray:(NSArray*)original {
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:[original count]];
for(UIImage *image in original){
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[temp addObject:thumb];
}
return [NSArray arrayWithArray:temp];
}
you might be able to use this?
yourTableViewController.rowImage = [UIImage imageNamed:#"yourImage.png"];
and/or
cell.image = yourTableViewController.rowImage;
and if your images are already 40x40 then you shouldn't have to worry about setting bounds and stuff... but, i'm also new to this, so, i wouldn't know, haven't played around with Table View row/cell images much
hope this helps.
I was able to make this work using interface builder and a tableviewcell. You can set the "Mode" properties for an image view to "Aspect Fit". I'm not sure how to do this programatically.
Try setting UIImageView.autoresizesSubviews and/or UIImageView.contentStretch.