How to take snapshot of app running on simulator? - ios

I used this code to take the snapshot of my app running on simulator, the quality of the snap is not good, Any idea?
-(IBAction)btnSave:(id)sender
{
UIGraphicsBeginImageContextWithOptions(Self.View.bounds.size,Self.View.opaque,10.0f);
[Self.View.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* Render the screen shot at custom resolution */
CGRect cropRect = CGRectMake(0,0,320,500);
UIGraphicsBeginImageContextWithOptions(cropRect.size,BrochureSave.opaque,10.0f);
[screenshot drawInRect:cropRect];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* Save to the photo album */
// UIImageWriteToSavedPhotosAlbum(customScreenShot , nil, nil, nil);
[self.library saveImage:customScreenShot toAlbum:#"Cemara roll" withCompletionBlock:^(NSError *error) {
if (error!=nil)
{
NSLog(#"Big error: %#",[error description]);
}
else
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Message" message:#"Image Save Cemara roll" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}

Try changing 10.0f to 0.0f in UIGraphicsBeginImageContextWithOptions:
here:
UIGraphicsBeginImageContextWithOptions(Self.View.bounds.size,Self.View.opaque,0.0f);

Related

How to check UIView is nil or not in objective c

I am new in iOS and I am facing problem regarding to do validation of view. I have created Signature like view
How to draw Signature on UIView, and now I want to do validation by using this code
-(IBAction)SavebtnClick:(id)sender
{
if(drawSignView==nil)
{
UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Pease Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertviewshow show];
}
else if (drawSignViewClient==nil)
{
UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Please Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertviewshow show];
}
else
{
viewcontroller1 *management =[[viewcontroller1 alloc] initWithNibName:#"viewcontroller1" bundle:nil];
[self.navigationController pushViewController:management animated:YES];
}
}
But I am not getting success. Please tell me what I am doing wrong.
I want to do validation.
if I have not sign it shows the message.
Please give me suggestion.
Thanks in advance!
I save Image and used condition
UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
[[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
// Store the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *imageData = UIImageJPEGRepresentation(viewImage, 100);
[defaults setObject:imageData forKey:#"image"];
[defaults synchronize];
if(viewImage==nil)
{
UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Pease Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertviewshow setTag:1];
[alertviewshow show];
}
But it not work because it contain blank image.
UIGraphicsBeginImageContextWithOptions(self.drawSignView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.drawSignView drawViewHierarchyInRect:self.drawSignView.bounds afterScreenUpdates:YES];
UIImage *signImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(signImage)
{
//Get Image from sign view Succesfully
}
else
{
UIAlertView *alertviewshow =[[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Please Sign" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertviewshow show];
}
Try this code its definatly work.
For that my suggestion is to check UIImage rather then UIView, That means are you getting the signature image or not. Then you should check like
if(singatureImage1 == nil){
}
else{
}
And for another signature
if(singatureImage2 == nil)
{
}
else{
}
And if you getting not nil image without sign it then use SinatureView which will give you nil image if you did't sign it then you can validate it.
If you check the UIView(i.e. Your signature view) is nil or not,You will get always not nil view because you have initialized it.
Please check this answer
UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
[[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((viewImage), 0.5)];
int imageSize = imgData.length;
NSLog(#"size of image in KB: %d ", imageSize/1024);
UIGraphicsBeginImageContext(self.drawSignViewClient.bounds.size);
[[self.drawSignViewClient.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImageClient = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
NSData *imgDataClient = [[NSData alloc] initWithData:UIImageJPEGRepresentation((viewImageClient), 0.5)];
int imageSizeClient = imgDataClient.length;
NSLog(#"size of image in KB: %d ", imageSizeClient/1024);
int OCS=imageSize/1024;
int Client=imageSizeClient/1024;
if(OCS<3)
{
alertviewshow=[[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Please do Signature " delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertviewshow setTag:1];
[alertviewshow show];
}
else if (Client<3)
{
alertviewshow=[[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Please do Signature " delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertviewshow setTag:1];
[alertviewshow show];
}
else
{
//Write Your Code....
}

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..

How to save image using AVCaptureDevice

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!

iOS : How to save current image from my current UIView to photo album? I'm stuck with UIImageWriteToSavedPhotosAlbum

I have my current image that can swipe back and forward. All the image are inside a folder called pages. So when I am in the current view, I have an IBAction to save that current image to photo album. How to implement that task ? I have study this but I don't know how to start and i'm stuck.
M file :
I define UIImage.h
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
);
- (IBAction)saveMe{
[UIImageWriteToSavedPhotosAlbum];
}
My Amendment code:
-(IBAction) saveToPhotoAlbum{
}
- (void) imageViewController:(UIImageView *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = //i have 6 current UIImageview on my current main view. How do I get stated?
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker release];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
draw uiview to context
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
and use UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); for save to album

Resources