Trying to save/load image in app error NSInvalidArgumentException - xcode

Im trying to save and load an image picked from camera roll within my app. I get an error when I press either the load or save button
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView CGImage]: unrecognized selector sent to instance 0xe637640'
any thoughts?
AppDelagate .m
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory i nDomains:NSUserDomainMask] lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
.m of class im trying to save/load image
- (IBAction)save:(id)sender {
UIImage *myImage = imageView;
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
}
- (IBAction)load:(id)sender {
UIImage *myImage = imageView;
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}

this error means that message CGImage is sent to object of type UIImageView, but UIImageView does not response to it. The problem is seemed to be in
UIImage *myImage = imageView;
You do not write properly implementation of imageView, so I can not say exactly, but I assume that imageView is UIImageView, which is subclass of UIView, and you are assigning it to UIImage. Try
UIImage *myImage = [imageView image];
instead. I think that should works))
by the way, why do you need first and second strings of code in -load? They are unnecessary:
- (IBAction)load:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}

Related

Store multiple image files in iPad Application

Should I store the multiple image files as NSData to SQLite or
save physical files to Document Folder??
Thanks
Store the image in the application directory. This is best and easy way to handle. Try the following code. it will be help you.
/Store Image/Songs files to Application Directory
+(BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return [data writeToFile:appFile atomically:YES];
}
//Image - Retrieve from Application Directory
+(NSData *)readFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
return myData;
}
return nil;
}

how to load pdf file in uiwebview from document directory in xcode?

i'm downloading my pdf file in document directory and then trying to show that pdf file
from that path to load in uiwebview but its not opening just a blank white page, i dont
understand why its not loading in uiwebview.
below is my code ,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#".pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
and my filepath is:
NSLog(#"Filepath------->> %#", filePath);
/Users/zee/Library/Application Support/iPhone Simulator/6.0/Applications/BD79829B-95FF-4B91-94AD-830C6B1CD31D/Documents/myPDF.pdf
Please tell me why its not working and what should be the best solution
Thanks
Zeeshan Shaikh
Try this,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#"sample.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

saving an image to documents directory - not working - only creating a zero byte file

I am trying to make a simple app to take a picture and save the file to both a UIImageview and to the documents folder so it can be recalled at a later time as the UIImageview. To get visibility into if the app is actually making a file, I have turned on Application supports iTunes File sharing within the .plist file.
Problem is the file generated is zero bytes. I have tried both PNG and JPG format.
- (IBAction)picImage:(id)sender {
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
//check to see if the device has camera capability
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else
{
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//set the UIImageView to the selected image
playerImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"player.png"];
UIImage *editedImage = [UIImage imageWithContentsOfFile:imagePath];
NSData *imageData = UIImagePNGRepresentation(editedImage);
//NSData *imageData = UIImageJPEGRepresentation(editedImage,1.0);
// This actually creates the image at the file path specified, with the image's NSData.
[[NSFileManager defaultManager] createFileAtPath:imagePath contents:imageData attributes:nil];
//dismiss the imagepicker view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Try this code and let me know is it working or not..!!!!
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *strimagename;
strimagename=[NSString stringWithFormat:#"Test.jpg"];
NSString *thumbFilePath = [documentsDirectory stringByAppendingPathComponent:strimagename];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *thumbData = UIImageJPEGRepresentation(image, 1);
[thumbData writeToFile:thumbFilePath atomically:YES];
[imgPicker dismissModalViewControllerAnimated:YES];
This code will save image in documentory folder..
Happy Coding..!!!!!!

How to add a jpg or png image to an UIImage and then display in a UIImageView?

UIImage *img = [[UIImage alloc] initWithContentsOfFile:#"contactHeader.png"];
_headersView = [[UIImageView alloc] initWithImage:img];
I have already hooked up the connections for the UIImageView (_headersView) in IB. The image I want to load is in my project tree structure as well.
Am I creating the UIImage the right way?
If the file is in your bundle you can just use
UIImage *img = [UIImage imageNamed:#"contactHeader"];
_headersView = [[UIImageView alloc] initWithImage:img];
If the image is in your documents directory you can use
// Get the path to your documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Add the file to the end of the documents path
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"contactHeader.png"];
UIImage *img = [[UIImage alloc] imageWithContentsOfFile:filePath];
_headersView = [[UIImageView alloc] initWithImage:img];
[img release]; img = nil;
try this
-(NSString *)getPath:(NSString *)filename
{
self.path = [[NSBundle mainBundle]pathForResource:filename ofType:#"png"];
return path;
path = nil;
}
self.youImageView.image = [UIImage imageWithContentsOfFile:[self getPath:#"123"]];

How to play video from NSData

I would like to know if it's possible to play a video from an NSData object... with the MPMoviePlayerController.
Ben's answer works perfectly on simulator but wont work on the device, You cannot write anywhere on the device. Check code below
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myMove.mp4"];
[videoData writeToFile:path atomically:YES];
NSURL *moveUrl = [NSURL fileURLWithPath:path];
player = [[MPMoviePlayerController alloc]init];
[player setContentURL:moveUrl];
player.view.frame = viewPlayer.bounds;
[viewPlayer addSubview:player.view];
[player play];
As far as I know this is not possible. If the data comes from your DB, can you save it into a temporary file and play that?
It is better to use NSFileManager in this case instead writeToFile
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myMove.mp4"];
[[NSFileManager defaultManager] createFileAtPath:path contents:videoData attributes:nil];
NSURL *moveUrl = [NSURL fileURLWithPath:path];
player = [[MPMoviePlayerController alloc]init];
[player setContentURL:moveUrl];
player.view.frame = viewPlayer.bounds;
[viewPlayer addSubview:player.view];
[player play];
create a file with the type of your NSData for example if your NSData is type of mp4 create a file with that type - for example - "myMove.mp4"
copy past the file to your app resurces
add this code
NSData *mediaData; //your data
NSString *movePath=[[NSBundle mainBundle] pathForResource:#"myMove" ofType:#"mp4"];
[mediaData writeToFile:movePath atomically:YES];
NSURL *moveUrl= [NSURL fileURLWithPath:movePath];
MPMoviePlayerController *movePlayer=[[MPMoviePlayerController alloc]init];
[movePlayer setContentURL:moveUrl];
[movePlayer play];

Resources