Still Image using AVCaptureDevice with overlay image - iphone

I am developing an iPhone app in which I am capturing the live video.
I have added the AVCaptureVideoPreviewLayer to the self.view.layer as sublayer.
[self.view.layer addSublayer: self.prevLayer];
On the same self.view I have added an image as subview.
[self.view addSubView:image];
Now what I want is to capture an image from the live video but the image should also come into that picture as it looks on the screen.
I have tried to take the screen shot but it capture only the image and not the live video image.
Can any body please help me on this.
Thanks in advance.

hi this will help you as it works for me
- (void)didTakePicture:(UIImage *)picture
{
UIImage *frm = self.overlayViewController.imgOverlay.image;
NSLog(#"width %f height %f",picture.size.width,picture.size.height);
UIGraphicsBeginImageContext(picture.size);
[picture drawAtPoint:CGPointMake(0,0)];
[frm drawInRect:CGRectMake(0, 0, picture.size.width, picture.size.height)];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(#"view %#",viewImage.debugDescription);
[self.capturedImages addObject:viewImage];
}

Related

How to take a screenshot from an video playing through MPMediaPlayerController in iPhone?

Can anybody help me in this? I want to get an screenshot from an video playing through MPMediaPlayerController. What i have tried so far is:-
-(void)viewDidLoad
{
NSURL *tempFilePathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"LMFao" ofType:#"mp4"]];
player = [[MPMoviePlayerController alloc] initWithContentURL:tempFilePathURL];
[player setControlStyle:MPMovieControlStyleFullscreen];
//Place it in subview, else it won’t work
player.view.frame = CGRectMake(0, 0, 320, 400);
[self.view addSubview:player.view];
}
-(IBAction)screenshot
{
CGRect rect = [player.view bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[player.view.layer renderInContext:context];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
[imgView setImage:image];
imgView.layer.borderWidth = 2.0;
[self.view addSubview:imgView];
}
And what i ve got now is this:-
what is happening now is my player's button are getting captured in ScreenShot but my video's image is not been getting.I'm using this context method to capture my image because
i have an overlay on my video,using thumbnail method i cant capture video with overlay but i want to get video's image with overlay.
EDIT:
what i want is getting screenshot of both this video and drawing overlay as shown in this image
But what i m getting is by one method i got only video not overlay in my screenshot that method is thumbnail method,i was doing thumbnailing in that which is given by MPMoviePlayerController and by second method i m getting only overlay not an video in ma screenshot,that method is context method.i'm getting context of rect.Now i thought solution for this may be is k i combine both these images.So help me out by giving suggestion is merging images will work for me or not???
Please help.Thanks :)
You just have to call a single method on your mpmovieplayer object:
- (UIImage *)thumbnailImageAtTime:(NSTimeInterval)playbackTime timeOption:(MPMovieTimeOption)option
this method will return a UIImage object. what you have to do is just:
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
for more details look at MPMoviePlayerController
Hope this helps
EDIT: In your method you can first hide player's control and than capture Image after that show controls again. you can achive this using controlStyle property of MPMoviePlayerController.
Not sure this is what you are looking for but it is what I understood. If you are looking for different let me know.
U can merge two image from this way.Please have an look from the below code..
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
MaskView=[[UIView alloc] init];
UIImageView *image1=[[UIImageView alloc]init];
UIImageView *image2=[[UIImageView alloc]init];
MaskView.frame=CGRectMake(0, 0,500,500);
image1.frame=CGRectMake(0,0,160, 160);
image2.frame=CGRectMake(60,54,40,40);
image1.image=image;
image2.image=maskImage;
[MaskView addSubview:image1];
[MaskView addSubview:image2];
UIGraphicsBeginImageContext(CGSizeMake(160,160));
[MaskView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext();
return finishedPic;
}
This may help
- (UIImage *)thumbnailImageAtTime:(NSTimeInterval)playbackTime timeOption:(MPMovieTimeOption)option
Get the thumbnailImage at the given time and scale that image.

UIGraphicsGetImageFromCurrentImageContext rotate the image at will?

In brief, the app is meant to do the following:
The user will select from the photos within the iPhone (via ALAssets). This part is fine.
The selected photo will appear on another view, within a smaller, rectangular subview.
When using the ALAsset thumbnail, the image shows up correctly within the subview. However, the resolution is poor, so I'm trying to use a higher resolution image.
When using the full resolution image, and then placing it into the view, any portrait photo will be rotated 90 degrees counter-clockwise.
Code-wise, this it looks like this:
//ImageView is the UIImageView subview that will hold the selected photo image.
//This code works ok, but I'd rather have a higher resolution photo.
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setImage:[UIImage imageWithCGImage:[selectedPhoto thumbnail]];
Now, this is the code where I try to use a higher resolution image, but then the image within imageView is rotated -90 degrees:
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
//HIGH RESOLUTION IMAGE
ALAssetRepresentation *rep = [selectedPhoto defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref)
{
UIImage *largeImage = [Utilities imageWithImage:[UIImage imageWithCGImage:iref] scaledToSize:CGSizeMake(CGImageGetWidth(iref)/4, CGImageGetHeight(iref)/4)];
[imageView setImage:largeImage];
}
And the imageWithImage: function called there looks like this:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
As mentioned, photos taken in landscape orientation by the iPhone camera come out OK. However, photos taken in portrait camera orientation come out rotated -90 degrees somehow.
How can I make it so that each photo, whether taken in portrait or landscape, will come out oriented correctly on my ImageView?
Any help would be appreciated! Thank you!
I found the answer here!
ALAssetRepresentation fullResolutionImage's UIImageOrientation is wrong
I am doing this now:
UIImage *largeImage = [[UIImage alloc] initWithCGImage:iref scale:4 orientation:(UIImageOrientation)rep.orientation];

Saving two Overlapping UIImage

I am trying to build a photo frame application on iphone. I made the frame it is transparent in png formate, then by choosing photos and was placed behind the frame layer in the interface builder.
In interface builder they are placed well and fit well. Now my problem is how can i save them into one picture.
Here is the code i have, but the saving part keep crashing.
-(IBAction) saveImage:(id)sender{
imagefront .backgroundColor = [UIColor clearColor]; //This sets your backgroung to transparent.
imagefront.opaque = NO;
[imageView bringSubviewToFront:imagefront];
UIImage *overlappedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(overlappedImage, self, #selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}
Imagefront is the photoframe while imageView is the photo.
Thank you.
Your current approach is incorrect. You will need to do this to get the image.
UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsGetCurrentContext();
This is assuming that imageView has imageFront as its subview as suggested by the code you've posted.

UIImageWriteToSavedPhotosAlbum creates black thumbnail in camera roll

I am saving image in the camera roll using UIImageWriteToSavedPhotosAlbum but always get an black thumbnail even if the picture is correct.
Do you have pointers to address this?
Thanks in advance for your help.
Regards,
I had the same problem. Drawing the image within a UIGraphicsImageContext solves the issue:
CGRect rect = CGRectMake(0,0,100,100);
UIImage *image = ((put here your image));
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(
result, self, #selector(image:didFinishSavingWithError:contextInfo:),nil);

iphone sdk: Creating an image of the contents of the screen?

I'm looking for a way to write the contents of the screen to an image. Any idea how to achieve that? Does it involve using Quartz?
Thanks
Add this code to your UIViewController to create a screen dump of its UIView.
// create screen dump of the view of this view controller
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// save image to photos
UIImageWriteToSavedPhotosAlbum(screenShot, self, nil, nil);
In a nutshell:
CGImageRef screen = UIGetScreenImage();
UIImage *screenImage = [UIImage imageWithCGImage:screen];