Objective-C: EXC_BAD_ACCESS reading NSData from file path - objective-c

I am trying to read NSData from a file path, and eventually unarchive the data.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
[[NSFileManager defaultManager] createDirectoryAtPath:[docs stringByAppendingString:#"/dynSplash"]
withIntermediateDirectories:NO
attributes:nil
error:nil];
NSString *dynamicSplashFile = [docs stringByAppendingString:#"/dynSplash/dynamicSplash.data"];
DynamicSplash *splash;
if([[NSFileManager defaultManager] fileExistsAtPath:dynamicSplashFile]) {
NSLog(#"Dynamic splash exists");
NSData *data = [NSData dataWithContentsOfFile:dynamicSplashFile];
splash = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
The problem arises on the line [NSData dataWithContentsOfFile:dynamicSplashFile] where a EXC_BAD_ACCESS error is bring thrown.
How can I debug this?

Related

I want to crete a zip file with multiple images in it and get the entire zip file and send to sftp server

I am trying create a zip file from the contents of another directory in NSdcomentDirectory. I am using "SSZipArchive" for this. But When I try to get the zip file its not available.Here is my code which I am trying.
`
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:#"/SourceFolder"];
NSString *stringPath2 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:#"/ZipFolder"];
//SourceFolder Creation
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
//ZipFolder Creation
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath2 withIntermediateDirectories:NO attributes:nil error:&error];
//Adding Files To SourceFolder
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:#"flower.jpg"], 1.0);
NSData *data2 = UIImageJPEGRepresentation([UIImage imageNamed:#"Wall.jpg"], 1.0);
[data writeToFile:[stringPath stringByAppendingFormat:#"/image1.jpg"] atomically:YES];
[data2 writeToFile:[stringPath stringByAppendingFormat:#"/image2.jpg"] atomically:YES];
//Retriving Path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *sampleDataPath = [documentsDirectory stringByAppendingPathComponent:#"/SourceFolder"];
NSString *sampleDataPath2 = [documentsDirectory stringByAppendingPathComponent:#"/ZipFolder"];
//To Create ZipFile under /ZipFolder from /SourceFolder
[SSZipArchive createZipFileAtPath: sampleDataPath2 withContentsOfDirectory: sampleDataPath];
//Checking If File exist in source and zip folder
NSFileManager *sharedFileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *files = [sharedFileManager enumeratorAtPath:sampleDataPath]; // The content shows two images which I have added ->image1.jpg,image2.jpg
NSDirectoryEnumerator *files2 = [sharedFileManager enumeratorAtPath:sampleDataPath2]; // However I dont see the .zip file inside it
`
I have below questions:
How can I get the source directory content as a zip file.
How to get the entire zip file so that I can send it to server as zip.
Please help me I am trying from a long time.

Objective C - How to move a file from a location to another?

Beginner at Objective C here, I have this code:
//Path of management log file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *urlPath= [documentsDirectory stringByAppendingPathComponent:#"Logs/log.txt"];
NSURL *source = [NSURL fileURLWithPath:urlPath];
//Destination
NSURL * destination = [self.pathControl URL];
//Move file from source to that destination directory.
[[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:nil];
The problem is that it doesn't do anything. It can compile, but nothing happens. Am I using copyItemAtURL method wrong?
Use to error indicators to understand errors:
NSError *error;
BOOL status = [[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:&error];
if (status == NO ) {
NSLog(#"errorL %#", error);
}

Comparing two NSData

In my project I need to compare if a website changed from now to 30 minutes later, so after a research, I arrived in this:
- (IBAction)saveHTML:(id)sender{
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
self.Surl= [self.meuWebView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
NSLog(#"URL: %#",self.Surl);
// Download and write to file
NSURL *url = [NSURL URLWithString:self.Surl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
// [web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
self.oldURL = urlData;
self.timerURLNew = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(saveHTMLNew:) userInfo:[NSNumber numberWithBool:YES] repeats:NO];
}
-(void)saveHTMLNew:(id)sender{
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
self.Surl= [self.meuWebView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
NSLog(#"%#",self.Surl);
NSURL *url = [NSURL URLWithString:self.Surl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
self.newURL = urlData;
if (![self.oldURL isEqualToData:self.newURL]) {
NSLog(#"Something changed");
}
else {
NSLog(#"Nothing changed");
}
}
The problem is that everytime I get "Something Changed". For testing I create a blog to post something to change its HTML.

trying to save image picked to appdocs-not happening

I'm really struggling with this,Day three of the unending quest to save and load an image in my app. I'm picking an image from camera roll and trying to save it to the device via the appsdocsdirectory.
in the Appdelegate.m I have:
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
}
in the class I want to save and load the image (picked from camera roll into UI imageView)
- (IBAction)save:(id)sender {
UIImage *myImage = [imageView image];
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
}
- (IBAction)load:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}
#end
I also imported Appdelegate.h, in this class, not sure if that was needed or correct?Now I have finally got rid of all errors and no exceptions being thrown but now my problem is nothing happens when I try to save the image and load it. I'm also getting yellow triangles telling me i have unused veriables in load UIImage* thumImage NSString *appDocsDirectory & NSData *data so I may have made a royal hash of this.
This is how I did it in one of my project.
I get the picture with the delegate method ot the imagePickerController :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
pictureInfo = info;
}
Then I save it like this :
-(void)createPin
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pinFolderName = [NSString stringWithFormat:#"%f",[[NSDate date] timeIntervalSince1970]];
NSString *pinFolderPath = [documentsDirectory stringByAppendingPathComponent:pinFolderName];
[fileManager createDirectoryAtPath:pinFolderPath withIntermediateDirectories:YES attributes:nil error:NULL];
self.fullsizePath = [pinFolderPath stringByAppendingPathComponent:#"fullsize.png"];
NSString *mediaType = [pictureInfo objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *fullsizeImage = [pictureInfo objectForKey:UIImagePickerControllerEditedImage];
NSData *fullsizeData = UIImagePNGRepresentation(fullsizeImage);
[fullsizeData writeToFile:self.fullsizePath atomically:YES];
}
}
Hope you can start from there.
If you need any explanations or help, feel free to ask
I have used NSuser defaults, all sorted

When I save image to file the orientation that save is wrong

When I save image to file it save with wrong orientation .
- (void)saveImage:(UIImage *)image:(NSString *)imageName
{
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"%#.png", imageName]];
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
}
Thanks.
Try saving as a jpeg instead:
NSData *imageData = UIImageJPEGRepresentation(image,1.0);
If you need to save it as a PNG, check this discussion:
UIImagePNGRepresentation ..... writeToFile is always landscape