The iPhone 5S is capable of taking pictures while recording video and I am trying to figure out how I would do this programatically. I know I would be utilizing AVFoundation, however, I couldn't find anything in the programming guide regarding this. I have also checked the sample projects (AVFoundation-related) and it doesn't look like there is anything there that does what I am looking for. if you could help point me in the right direction that would be great.
Actually you can do it with any device that can record video:
Create and configure a AVCaptureVideoDataOutput.
Add it to your AVCaptureSession.
Add a AVCaptureVideoDataOutputSampleBufferDelegate.
Implement captureOutput:didOutputSampleBuffer:fromConnection: in the delegate and get the image with imageFromSampleBuffer:.
Some similar code can be found here, where images are captured at a given interval, but you only want one image.
In iOS7 there has a new api to capture UIView, we can get image and do something.
eg:
UIView+Screenshot.h
-(UIImage *)convertViewToImage;
UIView+Screenshot.m
-(UIImage *)convertViewToImage
{
UIGraphicsBeginImageContext(self.bounds.size);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Apple's Document: drawViewHierarchyInRect:afterScreenUpdates:
Through AVCaptureSession you can easily achieve iPhone 5S in the camera app functionality.
In your view touch event:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection "
You can get current frame on above delegate method and save it in your gallery.
Please go through below link:
https://developer.apple.com/library/ios/samplecode/RosyWriter/Introduction/Intro.html
I agree with Rivera's answer ! You are going to need AV
And this is how I do it after the hole AV stuff
CGImageRef bigImage = image.CGImage;
CGRect rectImage = CGRectMake(self.scannArea.origin.x*widhtRatio,
self.scannArea.origin.y*heightRatio - 50,
self.scannArea.size.width*widhtRatio,
self.scannArea.size.height*heightRatio);
CGImageRef part = CGImageCreateWithImageInRect(bigImage, rectImage);
UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:part], nil, nil, nil);
image = [self drawImage:[UIImage imageNamed:#"overlaygraphic.png"]
inImage:image
atPoint:CGPointMake(self.scannArea.origin.x*widhtRatio,
self.scannArea.origin.y*heightRatio - 50)];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
if you want to see the hole sample np. I will upload my class
Related
Im developing an app where i have two images.user will touch some object on first image(for eg:nose).and then an object on the second image(eg:nose).
Now there will be button(merge button) and when user press that button i want to interchange the two touched images(ie two noses)of both images.
completely stuck at this point.
Can anyone please help me how to do this.
For detecting an image you can use Tap Gesture using which you can make your image draggable.
For merging an image You have to take all images you want to merge into a single UIView, for that checkout this snippet.
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
I'm able to crop a view with this code
- (UIImage *)captureScreenInRect:(CGRect)captureFrame {
CALayer *layer;
layer = self.view.layer;
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
}
But I have an imageview zoomed in with transform and it isn't shown to scale.
How do I capture EXACTLY what the user sees on the screen
The Stack Overflow question "renderInContext:" and CATransform3D has more info, but the gist is:
QCCompositionLayer, CAOpenGLLayer, and QTMovieLayer layers are not rendered. Additionally, layers that use 3D transforms are not rendered, nor are layers that specify backgroundFilters, filters, compositingFilter, or a mask values.
(from the CALayer docs).
More info is also available in this technical Q&A: http://developer.apple.com/library/ios/#qa/qa1703/_index.html
If your app is not going to the app store you can use the undocumented UIGetScreenImage API:
// Define at top of implementation file
CGImageRef UIGetScreenImage(void);
...
- (void)buttonPressed:(UIButton *)button
{
// Capture screen here...
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
// Save the captured image to photo album
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
(from John Muchow)
However, use of this API will make your app not get approved.
I have been unable to find any other workarounds.
I am right now on the phase of recollecting information for a new iPhone app. For this app, i would like to use the camera and photo image editing options. Is Apple offering API (controller) where i can use this integrated IOs features into my app? i mean, in a process that starts first using the iPhone camera (IOS feature), later using the photo editing options (IOS feature), compress and tagg it (personal features), and finally save it inside my personal app folder/library (not inside general photo library)?
I have been reading the UIImagePickerController class feature, but i would like to double check with you, before moving forward
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html
any idea for compressing the image or capturing it with less resolution?
thank you very much in advance
Instead of capturing the image with less resolution you can resize image in the callBack of UIImagePickerController which is
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *temp = (UIImage*)[info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImage *uploadImage = [self resizeImageWithImage:temp];
}
For resizing function :
- (UIImage*)resizeImageWithImage:(UIImage*)image {
CGSize newSize = CGSizeMake(newWidth, newHeight);
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
You may need to use :
#import <QuartzCore/QuartzCore.h>
and the library.
Also for image editing check for CoreImage library which you can get information from here
http://developer.apple.com/library/mac/#documentation/graphicsimaging/Conceptual/CoreImaging/ci_intro/ci_intro.html
For compressing this might help
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(image, 0.6);
//0 means most compression
//1 means least compression
}
UIImagePickerController will let user either select existing photo or take a new one (it depends on how you set up the controller). The only editing that this approach allows is for user to crop the image. Other than that, you'll have to provide your own features.
As for compressing the image, you can save it as JPG while defining the compression ratio like so:
NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
In my app, I need to save an image. I need the image to always be saved as a portrait, even if the device is in landscape mode. I am checking to see if the device is in landscape mode and if it is, I would like to rotate my image before it's saved as a PNG. Can anyone help me figure this out?
-(void) saveImage {
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
//// need to rotate it here
}
NSData *data = UIImagePNGRepresentation (viewImage);
[data writeToFile:savePath atomically:YES];
}
This thread may help you. It shows how to use the imageOrientation method of a UIImage in order to switch the orientation. Hope that Helps!
i want to take screenshots in landscape mode.
currently my below code takes screenshots in PORTRAIT mode.
also i want to store the images into the given location not in photo library..
how can i attain this...
thanks for any help
below is my code
UIGraphicsBeginImageContext(self.view.bounds.size) ;
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
As far as I know there is no way to take screenshots in landscape mode. Images taken with the iPhone contain an information called imageOrientation which is then used to rotate an UIImageView displaying that image.
But you should be able to take an image in portrait mode and rotate it by 90 degree before saving it. I can't try it right now but the following should work:
Create a method for rotation and pass the UIImage as an argument.
CGSize size = sizeOfImage;
UIGraphicsBeginImageContext(size);
CGContextRotateCTM(ctx, angleInRadians); // (M_PI/2) or (3M_PI/2) depending on left/right rotation
CGContextDrawImage(UIGraphicsGetCurrentContext(),
CGRectMake(0,0,size.width, size.height),
image);
UIImage *copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return copy;
To store into a given location you can use NSData as I have answered on your other question ( Saving images to a given location )