iOS: taking screenshot presenting video ipad - ios

I'm trying to take screenshot playing video but the screenshot always is showing blank. This is my code:
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(viewImage, 1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.jpg"];
[data writeToFile:filePath atomically:YES];
I'm using AVPlayer/AVPlayerLayer to play the video.
Any of you knows why this happening? or if I need to do something special with video to be able to take the screenshot?
I'll really appreciate your help.

I found a solution to my problem:
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:[self.player.currentItem asset]];
CGImageRef ref = [imageGenerator copyCGImageAtTime:self.player.currentItem.currentTime actualTime:nil error:nil];
UIImage *viewImage = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
NSData *data = UIImageJPEGRepresentation(viewImage, 1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.jpg"];
[data writeToFile:filePath atomically:YES];

Related

Save signature image in png format in ios

I have drawable text which is used for signature ,
I want this context save in png format with a specific path but I could not do this.I want my draw image save in png format and I can use any where
**Code for png format and path**
NSData *imageData = UIImagePNGRepresentation(_SignatureView.signatureImage);
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"/imageName.png"];
[imageData writeToFile:imagePath atomically:YES];
_SignatureView is a property and signatureImage is a method
**code of signature image method**
- (UIImage *)signatureImage
{
if (!self.hasSignature)
return nil;
UIImage *screenshot = [self snapshot];
return screenshot;
}
NSString *docPath= #"/Users/.../.../";
NSString *filePath=[docPath stringByAppendingPathComponent:#"PdfFileName.pdf"];
NSMutableData pdfData=[NSMutableData data];
CGRect bounds = CGRectMake(0, 0, 612, 792);
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPage();
[YourSignature.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
[pdfData writeToFile:filePath atomically:YES];
Try like this
- (void)saveImage: (UIImage*)image
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: #"test.png"] ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
and to get the image use the following code
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: #"test.png"] ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}

UIImages not recovered Once Device is Restarted

In iOS 8, I am writing UIImages on the device using the code
NSString *documentsDirectory = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *pathString = [NSString stringWithFormat:#"%#/%#%ld",documentsDirectory, #"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:#"PatientsId"]];
NSData *imageData = UIImageJPEGRepresentation(image, 40);////I have replaced it with UIImagePNGRepresentation as well
[imageData writeToFile: pathString atomically:YES];
The write to file is successful.
If I try to recover the image using the function,
NSArray *documentDirectories =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
// Get one and only document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
NSString *pathString =[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#%ld", #"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:#"PatientsId"]]] ;
UIImage *image = [UIImage imageWithContentsOfFile: pathString];
The image is recovered.
However, if I restart the device, the images are not there anymore. The function [UIImage imageWithContentsOfFile: pathString] returns nil and if I use NSData *myData = [[NSData alloc] initWithContentsOfFile: pathString];, the myData is nil as well.
This procedure was working perfectly fine prior to iOS 8, however, it isn't working properly anymore. Can someone please guide me what is wrong with this?

Received memory warning crash arc uiimageview

According to vm emulator image IO_PNG_Data exeeds limit and cause crashes. i am using following code to load image view.I have researched a lot but nothing helps.
CALayer *imageLayer = self.img_user.layer;
[imageLayer setBorderWidth:1];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:img_user.frame.size.width/2];
img_user.image =[UIImage imageWithContentsOfFile:getimgpath];
here getpath is the image at cashe, i have store the image get from url in cashe
NSString *stringURL = getpicurl;
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"userpic.png"];
[urlData writeToFile:filePath atomically:YES];
//obj_AppDelegate.img_path=filePath;
setimgpath(filePath);
}
While loading uiimageview each time memory exeeds,and crashes pls help.

How to get path of the image choose from gallery in iPhone?

I have Five buttons.On each button action.I have open gallery and choose an image.But the problem is on each button action same path is coming.
UIImage* image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
NSData* imageData = UIImagePNGRepresentation(image);
NSString * imageName = #"Myimage.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(#"fullPathToFile=%#",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
Set global variable initially int countVal = 1;
NSString * imageName = [NSString stringWithformat:#"%d.png",countVal]; // Imgae_name set here
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(#"fullPathToFile=%#",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
count +=1; // Increment count here
I may be missing something, but it looks like you're using the same name for the image each time: Myimage.png. Every time the image will be saved inside the documents directory with that same name. Maybe add a tag to each button and use that to distinguish each of the 5 different images?
May be it will helpful for u
[picker dismissModalViewControllerAnimated:YES];
imageView.image= [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(imageView.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:png];
[webData writeToFile:localFilePath atomically:YES];
NSLog(#"localFilePath.%#",localFilePath);
UIImage *image = [UIImage imageWithContentsOfFile:localFilePath];
OR
Use this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
imageView.image= [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:savedImagePath atomically:NO];
NSLog(#"localFilePath.%#",localFilePath);
If you still get it empty then try by checking if NSData is created properly
NSLog(#"webData.%#",webData);
OR
Try this: I hope defnetly it will be be helpful to you
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
//you can use UIImagePickerControllerOriginalImage for the original image
//Now, save the image to your apps temp folder,
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:#"upload-image.tmp"];
NSData *imageData = UIImagePNGRepresentation(img);
//you can also use UIImageJPEGRepresentation(img,1); for jpegs
[imageData writeToFile:path atomically:YES];
//now call your method
[self uploadMyImageToTheWebFromPath:path];
}
Try to use below code which just edited to you answer
The problem is only with saving image name,try to save it with different name
The below code will help you in saving image with diffrent name
note: put the tag value to diffrent button(5 buttons) and call bello method in its target
-(void)saveImage:(UIButton*)sender{
UIImage* image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
NSData* imageData = UIImagePNGRepresentation(image);
NSString * imageName =[NSString stringWithFormat:#"Myimage%d.png",sender.tag];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(#"fullPathToFile=%#",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
}
by this you can save image with different name

Saving image to Documents directory and retrieving for email attachment

I having trouble figuring out NSBundle & DocumentDirectory data, I have a Camera Picture "imageView" that I'm saving to the NSDocumentDirectoy and then want to retrieve it for attaching to an email,
Here the saving code:
- (IBAction)saveImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
Here is the new getting data code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
[picker addAttachmentData:myData mimeType:#"image/png" fileName:#"savedImage"];
- (IBAction)getImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
}
This should get you started!
Swift 3
// Create a URL
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let imageURL = documentsURL?.appendingPathComponent("MyImageName.png")
// save image to URL
let myImage = imageView.image! // or wherever you have your UIImage
do {
try UIImagePNGRepresentation(myImage)?.write(to: imageURL!)
} catch {}
// Use the URL to retrieve the image for sharing to email, social media, etc.
// docController.URL = imageURL
// ...
I force unwrapped some of the optionals for brevity. Use guard or if let in your code.
Because each iPhone app is in it's own sandbox, you don't have access to a device-wide documents folder. To attach an image to an email, save the image in your own documents folder. Try using [#"~/Documents" StringByExpandingTildeInPath] to get your local documents folder - that works for me. It looks like the technique you're using for attaching the image to an email is correct.
Hope that helps,
Try this one :
-(void)setProfilePic
{
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [docpaths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"Image.png"];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imagePath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
[profilePic_btn setBackgroundImage:thumbNail forState:UIControlStateNormal];
}

Resources