Custom made Camera Purposes Progress Using Apple iphone Sdk

Custom made Camera Purposes Progress Using Apple iphone Sdk

Apple iphone has many handy attributes. A person of them is create-in camera and Camera software system for creating pictures. It seems to be wonderful but what about camera usage with indigenous apps? Apple iphone SDK offers the capacity of working with camera through UIImagePickerController class. Which is wonderful but there is a tiny downside – you are unable to generate a entire-screen persistent “live” camera perspective like the Camera software does. As a substitute of that you must use UIImagePickerController only in modal method – demonstrate the pop-up modal perspective when you have to have a photo and shut the perspective just after the photo is created. You have to reopen this perspective once again to acquire the upcoming a person.

What’s more, that modal perspective has added panels and controls that overlay the camera perspective. Another downside is – you are unable to acquire a photo in a person touch you have to have to touch the Shoot button to acquire a photo and preview it, and then you have to have to touch the Help you save button to get the photo for processing. Possibly it is the very best apply but I really do not like it and I hope you consider the exact same way.

What about working with the UIImagePickerController as an ordinal non-modal perspective controller less than the navigation controller the exact same way as we use the other perspective controllers? Try it and you will observed that it operates! The camera perspective operates and seems to be as it must. You can assign a delegate and approach UIImagePickerControllerDelegate activities to get and help you save the photo. Okay, touch the Shoot button, touch the Help you save button – wonderful, you have acquired the photo! But just appear at this – the Retake and Help you save buttons remain over the camera perspective, and they really do not do the job now when they are touched… This is for the reason that you are unable to reset the perspective to acquire one more photo just after using a person and touching the Help you save button, the perspective is freezed and the buttons are disabled. It would seem you have to have to completely recreate the UIImagePickerController instance to acquire one more photo. Which is not so easy and not so fantastic. And you however have to have to use the panels and buttons that overlay the camera view…

Now I have an strategy! When we touch Shoot, the perspective stops refreshing and shows single graphic from the camera then we have to touch Retake or Help you save button. Can we get that graphic and help you save it with no working with the UIImagePickerControllerDelegate and then touch the Retake button programmatically to reset the perspective and get one more photo? Absolutely sure we can! If you examine the camera views hierarchy just after touching Shoot you will find that there is a concealed perspective of ImageView type. This class is not described in the SDK, but we can examine its’ solutions working with Goal-C abilities. We can see that the class has a system referred to as imageRef. Let’s check out this… Sure, it returns CGImage item! And the graphic size is 1200 x 1600 – it is undoubtedly the camera photo!

Okay, now we know we can get the photo with no UIImagePickerControllerDelegate. But in what moment must we do this? Can we capture the user touches on the Shoot button to begin processing? It is feasible but not so fantastic. Do you keep in mind our major reason – creating the persistent entire-screen camera perspective like system Camera software does? It is time to do it! When we explored the views hierarchy, we’ve observed that there are range of views over the camera perspective. We can check out to cover these views and generate our possess button below the camera perspective to acquire the photo in a person touch. But how can we pressure the camera perspective to make the photo? It is incredibly easy – we can get the corresponding selector from the Shoot button and get in touch with it from our action handler!

Okay, we’ve compelled finding the graphic. But it requires us few seconds. How can we detect that the graphic is completely ready? It occurred when the Cancel and Shoot buttons are changed by Retake and Help you save kinds. The most straightforward way to detect this is starting off a timer with quick interval and checking the buttons. And then we can get and help you save the photo, working with the corresponding selector from the Retake button and calling it to reset the camera perspective and put together it for creating a new a person. Below is the code:

// Shot button on the toolbar touched. Make the photo.
– (void)shotAction:(id)sender
[self enableInterface:NO]
// Simulate touch on the Image Picker’s Shot button
UIControl *camBtn = [self getCamShutButton]
[camBtn sendActionsForControlEvents:UIControlEventTouchUpInside]

// Established up timer to check out the camera controls to detect when the graphic
// from the camera will be well prepared.
// Image Picker’s Shot button is passed as userInfo to look at with present button.
[NSTimer scheduledTimerWithTimeInterval:.2 concentrate on:self selector:@selector(savePhotoTimerFireMethod:) userInfo:camBtn repeats:NO]

// Return Image Picker’s Shoot button (the button that makes the photo).
– (UIControl*) getCamShutButton

UIView *topView = [self findCamControlsLayerView:self.perspective]
UIView *buttonsBar = [topView.subviews objectAtIndex:2]
UIControl *btn = [buttonsBar.subviews objectAtIndex:one]

return btn

// Return Image Picker’s Retake button that seems just after the user pressed Shoot.
– (UIControl*) getCamRetakeButton

UIView *topView = [self findCamControlsLayerView:self.perspective]
UIView *buttonsBar = [topView.subviews objectAtIndex:2]
UIControl *btn = [buttonsBar.subviews objectAtIndex:]

return btn

// Uncover the perspective that has the camera controls (buttons)
– (UIView*)findCamControlsLayerView:(UIView*)perspective

Course cl = [perspective class]
NSString *desc = [cl description]
if ([desc look at:@”PLCropOverlay”] == NSOrderedSame)
return perspective

for (NSUInteger i = i < [view.subviews count] i++) UIView *subView = [perspective.subviews objectAtIndex:i]
subView = [self findCamControlsLayerView:subView]
if (subView)
return subView

return nil

// Identified as by the timer. Look at the camera controls to detect that the graphic is completely ready.
– (void)savePhotoTimerFireMethod:(NSTimer*)theTimer

// Review present Image Picker’s Shot button with passed.
UIControl *camBtn = [self getCamShutButton]
if (camBtn != [theTimer userInfo])

// The button changed by Help you save button – the graphic is completely ready.
[self saveImageFromImageView]

// Simulate touch on Retake button to go on working the camera is completely ready to acquire new photo.
camBtn = [self getCamRetakeButton]
[camBtn sendActionsForControlEvents:UIControlEventTouchUpInside]

[self enableInterface:Sure]

else

NSTimeInterval interval = [theTimer timeInterval]
[NSTimer scheduledTimerWithTimeInterval:interval concentrate on:self selector:@selector(savePhotoTimerFireMethod:) userInfo:camBtn repeats:NO]

// Help you save taken graphic from concealed graphic perspective.
– (BOOL)saveImageFromImageView

UIView *cameraView = [self.perspective.subviews objectAtIndex:]
if ([self enumSubviewsToFindImageViewAndSavePhoto:cameraView])
return Sure

return NO

// Recursive enumerate subviews to find concealed graphic perspective and help you save photo
– (BOOL)enumSubviewsToFindImageViewAndSavePhoto:(UIView*)perspective

Course cl = [perspective class]
NSString *desc = [cl description]
if ([desc look at:@”ImageView”] == NSOrderedSame)
return [self grabPictureFromImageView:perspective]

for (int i = i < [view.subviews count] i++) if ([self enumSubviewsToFindImageViewAndSavePhoto:[perspective.subviews objectAtIndex:i]])
return Sure

return NO

// Grab the graphic from concealed graphic perspective and help you save the photo
– (BOOL)grabPictureFromImageView:(UIView*)perspective

CGImageRef img = (CGImageRef)[perspective imageRef]
if (img)

// Taken graphic is in UIImageOrientationRight orientation
UIImage *photo = [self correctImageOrientation:img]
UIImageWriteToSavedPhotosAlbum(photo, nil, nil, nil)

return Sure

return NO

// Suitable graphic orientation from UIImageOrientationRight (rotate on 90 levels)
– (UIImage*)correctImageOrientation:(CGImageRef)graphic

CGFloat width = CGImageGetWidth(graphic)
CGFloat top = CGImageGetHeight(graphic)
CGRect bounds = CGRectMake(.0f, .0f, width, top)

CGFloat boundHeight = bounds.size.top
bounds.size.top = bounds.size.width
bounds.size.width = boundHeight

CGAffineTransform renovate = CGAffineTransformMakeTranslation(top, .0f)
renovate = CGAffineTransformRotate(renovate, M_PI / 2.0f)

UIGraphicsBeginImageContext(bounds.size)

CGContextRef context = UIGraphicsGetCurrentContext()

CGContextScaleCTM(context, – one.0f, one.0f)
CGContextTranslateCTM(context, -top, .0f)
CGContextConcatCTM(context, renovate)

CGContextDrawImage(context, CGRectMake(.0f, .0f, width, top), graphic)

UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return imageCopy

Another vital concern is: in what moment can we cover the overlaying camera views and controls and generate our possess button? Attempting the viewDidLoad… Oops… The camera perspective is however not made. Attempting the viewWillAppear… The exact same thing… Attempting the viewDidAppear… Sure, the views have been made and can be concealed now. Okay, we cover that and generate a toolbar with our Shoot button. It operates, but the screen flicks – we see how the conventional views and buttons are demonstrated and then concealed. How can we protect against this? I tried a range of approaches and had observed the very best a person: we must cover the views right before they are added to the camera perspective (when the addSubview system of the camera perspective is referred to as). It is feasible working with Goal-C capacity to change the system dynamically at operate-time. Okay, let’s change the addSubview by our possess system. In our system we can check out that the passed perspective is a person of the camera perspective subviews and set its’ “hidden” home to Sure. So, we change the addSubview in the viewWillAppear right before the camera perspective is made. And we generate our toolbar and Shoot button in the viewDidAppear just after the camera perspective is made. Get a appear at the code below:

// Swap “addSubview:” if referred to as first time cover camera controls usually.
– (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated]

if (toolBar != nil)
{
// The perspective was previously appeared we do not have to have to subclass UIView
// but have to have to cover further camera controls.
UIView *cameraView = [self findCamControlsLayerView:self.perspective]
if (cameraView)

cameraView = cameraView.superview
int cnt = [cameraView.subviews rely]
if (cnt >= four)

for (int i = 2 i < cnt - 1 i++) UIView *v = [cameraView.subviews objectAtIndex:i]
v.concealed = Sure

}
else

// Subclass UIView and change addSubview to cover the camera perspective controls on fly.
[RootViewController exchangeAddSubViewFor:self.perspective]

}

// Trade addSubview: of UIView class set our possess myAddSubview as an alternative
+ (void)exchangeAddSubViewFor:(UIView*)perspective

SEL addSubviewSel = @selector(addSubview:)
Approach originalAddSubviewMethod = class_getInstanceMethod([perspective class], addSubviewSel)

SEL myAddSubviewSel = @selector(myAddSubview:)
Approach replacedAddSubviewMethod = class_getInstanceMethod([self class], myAddSubviewSel)

system_exchangeImplementations(originalAddSubviewMethod, replacedAddSubviewMethod)

// Insert the subview to perspective “self” factors to the dad or mum perspective.
// Established “concealed” to Sure if the subview is the camera controls perspective.
– (void) myAddSubview:(UIView*)perspective

UIView *dad or mum = (UIView*)self

BOOL finished = NO
Course cl = [perspective class]
NSString *desc = [cl description]

if ([desc look at:@”PLCropOverlay”] == NSOrderedSame)

for (NSUInteger i = i < [view.subviews count] i++) UIView *v = [perspective.subviews objectAtIndex:i]
v.concealed = Sure

finished = Sure

[RootViewController exchangeAddSubViewFor:dad or mum]

[dad or mum addSubview:perspective]

if (!finished)
[RootViewController exchangeAddSubViewFor:dad or mum]

The system described over was used in iUniqable software out there from Apple App Keep (Social Networking segment). Truly feel free of charge to use.

Truly feel free of charge to go to the website of the developer www.enterra-inc.com

Comments are closed.