How to save image using AVCaptureDevice - ios

I'm using Apple's GLCameraRipple example code in my project and I was wondering if there is a way to take a pic/video? Project can be found at https://developer.apple.com/library/ios/samplecode/GLCameraRipple/Introduction/Intro.html

First of all, ensure that you have included AssetLibrary framework, because we need that to access the photo library. Assuming that you have set up the capture AVCaptureSession, AVCaptureDevice, and AVCaptureStillImageOutput (stillImage) correctly, now you can create a button or simply call the below function to save an image.
-(void)captureMultipleTimes
{
AVCaptureConnection *connection = [stillImage connectionWithMediaType:AVMediaTypeVideo];
typedef void(^MyBufBlock)(CMSampleBufferRef, NSError*);
MyBufBlock h = ^(CMSampleBufferRef buf, NSError *err){
NSData *data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buf];
[self setToSaveImage:[UIImage imageWithData:data]];
dispatch_async(dispatch_get_main_queue(), ^{
if(saveLabel == NULL){
[self setSaveLabel:[[UILabel alloc] initWithFrame:CGRectMake(0,self.view.bounds.size.height/2, self.view.bounds.size.width, 50)]];
[saveLabel setText:#"Saving.."];
[saveLabel setTextColor:[captureBt titleColorForState:UIControlStateNormal]];
[self.view addSubview:saveLabel];
} else
saveLabel.hidden = NO;
UIImageWriteToSavedPhotosAlbum(toSaveImage, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
});
};
[stillImage captureStillImageAsynchronouslyFromConnection:connection completionHandler:h];
}
You also need to implement the image:didFinishSavingWithError:contextInfo: method as the completion function of saving image. One example is as follows:
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if(error != NULL){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error!" message:#"Image could not be saved" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else{
[saveLabel setHidden:YES];
}
}
The functions above will display "Saving.." label on the screen once you trigger the captureMultipleTimes function. It simply means that it is currently saving the video input as an image and store it into the photo lib. Once it has finished saving, the saving label will be hidden from the screen. Hope this helps!

Related

NSoperation - string not visible outside the block

I am currently working on a project and using tesseract API .
The code is following :
UIImage *bwImage = [image g8_blackAndWhite];
[self.activityIndicator startAnimating];
// Display the preprocessed image to be recognized in the view
self.imageView.image = bwImage;
G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] init];
operation.tesseract.engineMode = G8OCREngineModeTesseractOnly;
operation.tesseract.pageSegmentationMode = G8PageSegmentationModeAutoOnly;
operation.delegate = self;
operation.recognitionCompleteBlock = ^(G8Tesseract *tesseract) {
// Fetch the recognized text
NSString *recognizedText = tesseract.recognizedText;
NSLog(#"%#", recognizedText);
[self.activityIndicator stopAnimating];
// Spawn an alert with the recognized text
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"OCR Result"
message:recognizedText
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
};
//NSLog(#"%#",);
// Finally, add the recognition operation to the queue
[self.operationQueue addOperation:operation];
}
I want to pass recognizedText string to second View controller but it is not visible outside the block.
How can I achieve this, any advice?
Declare recognizedText outside block with __block keyword to make it visible outside block.
Like below code:
......
__block NSString *recognizedText;//declared outside to make it visible outside block
operation.recognitionCompleteBlock = ^(G8Tesseract *tesseract) {
// Fetch the recognized text
recognizedText = tesseract.recognizedText;
NSLog(#"%#", recognizedText);
[self.activityIndicator stopAnimating];
// Spawn an alert with the recognized text
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"OCR Result"
message:recognizedText
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
};
......

How to play recored video output url in the table view

I have a CollectionView with images and in the next ViewController I am recording a video using Av captured sessions and i am saving it in to the mobile document directory,Then i am coming back to the collection and i am reloading the collection view now i want the recored video in the collection view cell with out using any api.
I used the below code to save the recored video in the directory
'- (void)saveRecordedFile:(NSURL *)recordedFile {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary writeVideoAtPathToSavedPhotosAlbum:recordedFile
completionBlock:
^(NSURL *assetURL, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *title;
NSString *message;
if (error != nil) {
title = #"Failed to save video";
message = [error localizedDescription];
}
else {
title = #"Saved!";
message = nil;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
UIStoryboard *storybord=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
shareViewController *shareview=[storybord instantiateViewControllerWithIdentifier:#"share"];
[self presentViewController:shareview animated:YES completion:nil];
});
}];
});
can any body help out to solve this problem.solution view code is appreciated. i recored video by referring this https://github.com/shu223/SlowMotionVideoRecorder
1) You can achieve it by getting the center frame of the video which you have stored in the document directory.
2) You can store the center frame image of the video to document directory so that you don't have to generate everytime.
3) Display video thumbnail inside collectionview.
Refer this link for get frame of the video :
Create thumbnail from a video url in IPhone SDK
EDIT:
NSString *videoPath = [videoURL path];
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:videoPath]];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView]; [videoPlayerView.moviePlayer play];

how to Pass recordedFile to collection view cell either by block completion or notification

Hi I have an video recored output and i want to parse it to the other controller using either blocks or nsnotification center. Here is the code used to get output.
- (void)saveRecordedFile:(NSURL *)recordedFile {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary writeVideoAtPathToSavedPhotosAlbum:recordedFile
completionBlock:
^(NSURL *assetURL, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *title;
NSString *message;
if (error != nil) {
title = #"Failed to save video";
message = [error localizedDescription];
}
else {
title = #"Saved!";
message = nil;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
NSLog(#"%#",recordedFile);'
//
}
This output i have to pass to next view controller and from this to time line same like instagram app.
one more thing is already collection view is having images
some body please help me to solve this problem

How can i simply used progress view in my app

I am trying to save image to the photos album from my app using the following code:
-(void)download:(UIButton *)downloadbtn{
j=320*downloadbtn.tag;
selectedIndex=downloadbtn.tag;
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[live_god_img objectAtIndex:selectedIndex]]];
UIImage*img=[UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(img, self, nil, nil);
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:#"Success..!" message:#"Image Saved to the Photos" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
When the image is saved to the photos album, an alert view will be shown, but I would like to show a progress view while image is being saved to the album. I do not know how to use UIProgressView. How do I do it?
Go through this tutorial first
You can try like this in your method.
- (IBAction)startProgress:(id)sender
{
progressValue = 0.0f;
[self increaseProgressValue];
}
-(void)increaseProgressValue
{
if(progressView.progress<1)
{
progressValue = progressValue+0.1;
progressView.progress = progressValue;
[self performSelector:#selector(increaseProgressValue) withObject:self afterDelay:0.2];
}
}

Saving image to camera roll iOS works in simulator but not on phone

I'm using the following code to save a screenshot from a Cocos2d layer to the camera roll..
-(void) takeScreenshot {
[self turnAllNotesBlack];
CCScene *scene = [[CCDirector sharedDirector] runningScene];
CCNode *n = [scene.children objectAtIndex:0];
UIImage *img = [self screenshotWithStartNode:n];
//NSParameterAssert(img);
UIImageWriteToSavedPhotosAlbum(img,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
[self resetIlluminatedNotes];
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Save failed"
message: #"Failed to save image"
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
It works fine on the simulator, but when I run it on my phone, it calls the error method, and no picture is saved.
Does anyone know why this is?
Carl
Ahh the answer lies in the phone itself..
I had to go to 'Settings -> Privacy -> Photos' and enable photos for my new App..

Resources