Within my App I'm using the Camera to take several pictures. These pictures need to be sended in one time. For that reason I'm trying to implement a function to compress the photo's I took.
I've been looking on the internet for several libraries which I could use within my project. I came out with Objective Zip (http://code.google.com/p/objective-zip/wiki/GettingStarted). However the only thing I succeeded was compressing (or decompressing) text-files. But compressing (or decompressing) photo's is not mentioned.
I've succeeded with converting my token photo's to a NSData object. Now i want to compress them to a Zip file. Is there anyone who have had experience with these kind of functions.
Help is greatly appreciated!
Edit
I'm now using the Objective Zip library but my App keeps crashing. Still getting the known error "Thread 1: Program reveived signal : "SIGARBT" ". I'm trying to zip one image I took with the camera and stored in the documents directory.
Within the snippet you can see that I'm calling the photo I took and zipping it with the ZipFile method. In the end I'm sending my zip-file as an email attachment.
Here's a snippet of my code:
-(IBAction)sendID{
NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docDir4 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath3 = [NSString stringWithFormat:#"%#/photo.png",docDir3];
// Create the zip file
ZipFile *zipFile = [[ZipFile alloc] initWithFileName:docDir4 mode:ZipFileModeCreate];
// Read the files in the data directory
NSError *error = nil;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pngFilePath3 error:&error];
//ZipWriteStream *stream1= [zipFile writeFileInZipWithName:#"abc.txt" fileDate:[NSDate //dateWithTimeIntervalSinceNow:-86400.0] compressionLevel:ZipCompressionLevelBest];
// Write each file to our zip
for (NSString *filename in files) {
// Write the file
ZipWriteStream *stream = [zipFile writeFileInZipWithName:filename compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:pngFilePath3];
[stream writeData:data];
[stream finishedWriting];
}
// Close the zip file
[zipFile close];
NSData *data = [NSData dataWithContentsOfFile:#"Test.zip"];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate=self;
//NSData *imageData = UIImageJPEGRepresentation(viewImage, 1);
[picker addAttachmentData:data mimeType:#"application/zip" fileName:#"Test.zip"];
Class mailclass = (NSClassFromString(#"MFMailComposeViewController"));
if([mailclass canSendMail]){
[self presentModalViewController:picker animated:YES];
}
}
Hope anyone can help me out.
I know this is an old thread, but here's how I zip up multiple files using Objective-Zip:
- (NSString*) objectiveZip:(NSArray*)passedFiles
{
NSString *outputPath = [self getOutputDirectory];
NSString *zipFileName = #"MyZip.zip";
outputPath = [outputPath stringByAppendingString:zipFileName];
//Create a zip file for writing
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:outputPath mode:ZipFileModeCreate];
//Add all the files, write to its stream and close it
for(NSString *string in passedFiles)
{
NSString *fileName = string;
NSLog(#"Add file to zip: %#", fileName);
ZipWriteStream *stream1= [zipFile writeFileInZipWithName:fileName fileDate:[NSDate dateWithTimeIntervalSinceNow:-86400.0] compressionLevel:ZipCompressionLevelBest];
[stream1 writeData:[NSData dataWithContentsOfURL:[NSURL URLWithString:info.pdfUrl]]];
[stream1 finishedWriting];
}
// Close the zip file
[zipFile close];
return outputPath;
}
The best zip lib I've found is SSZipArchive. Give that one a shot.
NSString *temp=[[[makeZip textFieldAtIndex:0]text]stringByAppendingPathExtension:#"zip"];
NSString *zippedPath = [pathString stringByAppendingPathComponent:temp];
NSMutableArray *inputPaths=[[NSMutableArray alloc]init];
for (int i=0; i<[selectedRows count]; i++) {
[inputPaths addObject:[pathStringstringByAppendingPathComponent:[selectedRows objectAtIndex:i]]];
}
if (![[NSFileManager defaultManager] fileExistsAtPath:zippedPath])
{
[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];
[directoryContents addObject:temp];
[tblview reloadData];
}
Related
In my application I want to implement a simple Alarm function. I know how to use UILocalNotifications, but I came across this source code with a like UI of the iPhone's native Clock app alarm area as well as it having a believe a type of data persistence. Two things I am not good at interface design and data persistence this source code has. But I downloaded it and started playing around with it to find the alarms are not persistent.
Download
Does anyone know how the source code can be adjusted so that it is persistent and the plist can be saved and read to and from? I am open to learning too, this area is somewhat unknown to me too. Thanks
I review your code and find issue that you not moved your "Alarms.plist" file form resource to document directory. we are not able to edit file which is in resource folder. so write following code in app delegate file.
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *theFileName = #"Alarms.plist"; //Change this appropriately
NSString *oldPath = [[NSBundle mainBundle] pathForResource:#"Alarms" ofType:#"plist"];//[NSString stringWithFormat:#"%#/Inbox/%#", documentsDirectory, theFileName];
NSString *newPath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, theFileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:newPath])
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];
Perform save operation on file which is in Document directory folder.
try this code... to save plist from bundle to Document Directory
Notice that you will have "Unable to read... " just at the first app launch
- (NSMutableArray *)displayedObjects
{
if (_displayedObjects == nil)
{
NSString *path = [[self class] pathForDocumentWithName:#"Alarms.plist"];
NSArray *alarmDicts = [NSMutableArray arrayWithContentsOfFile:path];
if (alarmDicts == nil)
{
NSLog(#"Unable to read plist file: %#", path);
NSLog(#"copy Alarms.plist to: %#", path);
NSString *pathToSetingbundle = [[NSBundle mainBundle] pathForResource:#"Alarms" ofType:#"plist"];
[[NSFileManager defaultManager]copyItemAtPath:pathToSetingbundle toPath:path error:nil];
}
_displayedObjects = [[NSMutableArray alloc]
initWithCapacity:[alarmDicts count]];
for (NSDictionary *currDict in alarmDicts)
{
Alarm *alarm = [[Alarm alloc] initWithDictionary:currDict];
[_displayedObjects addObject:alarm];
NSLog(#"#disply obj %#", alarm);
}
}
return _displayedObjects;
}
I am fetching URL of the images from the server and converting this images into png format using:
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(img)];
[data1 writeToFile:pngFilePath atomically:YES];
but some of the images are corrupted when i check them after completing the process on simulator.
Hence these images are not displaying on the app wherever needed.
Please see the attached image as some images are corrupted.
Update
I am calling a method in a loop which fetches the images from the server parallel and didFinishLoading I am performing this:
UIImage *img = [UIImage imageWithData:self.data];
NSArray *split = [self.strImageName componentsSeparatedByString:#"/"];
int arrCount=[split count];
NSString *imageName=[split objectAtIndex:arrCount-1];
NSString *docDirec = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath=nil
pngFilePath = [NSString stringWithFormat:#"%#/Thumbs/%#",docDirec,imageName];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(img)];
[data1 writeToFile:pngFilePath atomically:YES];
[self.data release]; //don't need this any more, its in the UIImageView now
self.data=nil;
i have the similar problem
i solved it using the below code
- (void)viewWillDisappear:(BOOL)animated
{
if (self.m_objWebManager != nil)//Webmanager is class for downloading the images as a background thread
{
[self.m_objWebManager cancelCommunication];
[self.m_objWebManager release];
self.m_objWebManager = nil;
}
}
I am having an app in which at the time of launcing the app XML parsing is giving Main category from URL like hp, dell, etc...I am displaying it in the Tableview.
Then on click of particular cell i can get the detail of main category means its subcategory like http://www.dealsbell.com/findcoupon.php?store=hp
Here also i am getting data properly after parsing.
But my concern over here is, in ( http://www.dealsbell.com/findcoupon.php?store=hp ) this link i am getting images.
Each particular subcategory will have a same image. So i want to do something like that the image if first time loaded from the URL then it will display image from parsing otherwise i would like to store that image as its byte code in folder / file / in any way in my device on first parsing.
If once the image is stored to the particular way in my device next time when i will go to see the subcategory it will first check this image is stored locally to my device or not.
If yes then it should go to the particular location to fetch this local image & display it to each cell otherwise will parse & display image.
I hope you are getting, what i want to ask.
Please guide me, how can this be possible & what is the way to get result.
If any example or link you can suggest, then it will be more efficient to me.
Thanks in advance.
There are probably two ways to achive this.
Get NSData out of Image and hold that in UserDefaults or database.
Dump image in application folder and pick image from that place.
So whenever you try to load image for subcatogory check at one of place and if present use that. IF in case you have stored image and if any updated image comes,then remove previous copy and store new one.
-(void) SaveImageinDocumentWithName:(UIImage*) aUIImage Name:(NSString*) aName
{
if(aUIImage)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSMutableString* str = [[NSMutableString alloc] initWithCapacity:300];
[str appendString:documentsDirectory];
[str appendString:#"/"];
[str appendString:aName];
NSFileManager *fileManager = [NSFileManager defaultManager];
[UIImagePNGRepresentation(aUIImage) writeToFile:str atomically:YES];
if(str)
{
[str release];
str = nil;
}
if(fileManager)
{
[fileManager release];
fileManager = nil;
}
[pool release];
}
}
-- Getting saved image
-(UIImage*)GetSavedImageWithName:(NSString*) aFileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSMutableString* str = [[NSMutableString alloc] initWithCapacity:300];
[str appendString:documentsDirectory];
[str appendString:#"/"];
[str appendString:aFileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:str];
NSData *dataToWrite = nil;
UIImage* image = nil;
if(!success)
{
}
else
{
image = [[UIImage alloc] initWithContentsOfFile:str];
}
if(dataToWrite)
{
[dataToWrite release];
dataToWrite = nil;
}
if(str)
{
[str release];
str = nil;
}
if(fileManager)
{
[fileManager release];
fileManager = nil;
}
return image;
}
Parse dealsbell.com/wp-content/uploads/mobile/hp.gif and take only hp.gif
NSString *strImage = [NSString stringWithFormat:#"%#",aBook.image];
UIImage* image = [self GetSavedImageWithName:strImage];
if(image) // This means Image exists
{
// Do what you want
}
else
{
NSURL *url4Image = [NSURL URLWithString:strImage];
NSData *data = [NSData dataWithContentsOfURL:url4Image];
if(data != NULL)
{
image =[[UIImage alloc] initWithData:data];
[self SaveImageinDocumentWithName:image Name:strImage]; // save for future ref.
}
else
{
image =[UIImage imageNamed:#"icon.png"];
}
}
As a practice, I am trying to write an app similar to the built-in notes app.
But I cannot figure out how to save the file and display it in a UITableView.
Right now, I have a UITextView that the user can type in. I also have a save button.
When the user taps the save button, I want to save it, and later have it displayed in a table view.
I am very lost so if you know of any relevant tutorials etc. it would be greatly appreciated.
As noted by the commenters in the real world, you're definitely going to want to look at Core Data or some other data persistence strategy. If you're dead set on pursuing this as a learning experience, something like this should solve your problem:
- (void)writeStringToFile:(NSString*)aString {
// Build the path, and create if needed.
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = #"myTextFile.txt";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
[[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
}
// The main act...
[[aString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
}
- (NSString*)readStringFromFile {
// Build the path...
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = #"myTextFile.txt";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
// The main act...
return [[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding] autorelease];
}
The easiest way to save text is using NSUserDefaults.
[[NSUserDefaults standardUserDefaults] setObject:theText forKey:#"SavedTextKey"];
or, if you want to have the user name each "file" or be able to have multiple files
NSMutableDictionary *saveTextDict = [[[[NSUserDefaults standardUserDefaults] objectForKey:#"SavedTextKey"] mutableCopy] autorelease];
if (saveTextDict == nil) {
saveTextDict = [NSMutableDictionary dictionary];
}
[saveTextDict setObject:theText forKey:fileName];
[[NSUserDefaults standardUserDefaults] setObject:saveTextDict forKey:#SavedTextKey"];
I want to grab all URL's accessed in webview and write them to a text file.I don't want to use writeToFile coz in -(BOOL)webView method it would overwrite instead of appending to file. I can create file but all it writes to that file is string I used to create the file using FileManager createFileAtPath with Content ...
Also in the -(BOOL) webView method... when I tried to see if the file is read-only or writable ( isWritableFileAtPath ) it gives read-only.
POSIX Permissions in viewDidLoad -> 511 ...
checked file attributes in terminal going to that location its -rwxrwxrwx
I am new to Stack Overflow don't know how to post code here so using pastebin...http://pastebin.com/Tx7CsXVB
- (void)viewDidLoad {
[super viewDidLoad];
[myBrowser loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com"]]]; // UIWebView *myBrowser; is an instance variable
NSFileManager *fileMgr=[NSFileManager defaultManager];
NSDictionary* fileAttrs = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:777] forKey:NSFilePosixPermissions]; /*for setting attribute to rwx for all users */
NSDictionary *attribs; // To read attributes after writing something to file
NSString *aPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/file1.txt"];
myBrowser.delegate = self; // UIWebView delegate Self
// if the File Doesn't exist create one
if([fileMgr fileExistsAtPath:aPath])
{
NSLog(#"File Exists at this Location");
}
else
{
NSString *someString = #"This is start of file";
NSData *startString =[someString dataUsingEncoding: NSASCIIStringEncoding];
[fileMgr createFileAtPath:aPath contents:startString attributes: fileAttrs]; // earlier attributes was nil changed to fileAttrs
}
NSLog(#"aPath is %#",aPath);
attribs = [fileMgr attributesOfItemAtPath:aPath error: NULL];
NSLog (#"Created on %#", [attribs objectForKey: NSFileCreationDate]);
NSLog (#"File type %#", [attribs objectForKey: NSFileType]);
NSLog (#"POSIX Permissions %#", [attribs objectForKey: NSFilePosixPermissions]);
}
//UIWebView delegate calls this method every time user touches any embedded URL's in the current WebPage. I want to grab all the URL's accessed and write them to file.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSFileManager *fileManager =[NSFileManager defaultmanager];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/file1.txt"];
fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
[fileHandle seekToEndOfFile]; // Moved up next to fileHandleForWritingAtPath since the above would place pointer to start of file again so setting handle to seek to End of File
/* section of code to check if the file at that path is writable or not */
if ([fileManager isWritableFileAtPath:path] == YES)
NSLog (#"File is writable");
else
NSLog (#"File is read only");
/* section of code to check if the file at that path is writable or not ENDS*/
NSURL *url = request.URL;
NSString *currenturl = url.absoluteString;
NSString *currentURL = [NSString stringWithFormat:#"%#\n",currenturl];
NSString *str =[NSString stringWithFormat:#"%#",currentURL];/* has already been set up */
[fileHandle writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
// testing if string has been written to file by reading it...
NSData *dataBuffer = [fileMgr contentsAtPath:path];
NSString *some;
some = [[NSString alloc] initWithData:dataBuffer encoding:NSASCIIStringEncoding];
NSLog(#"SOme String is: %#",some);
[fileHandle closeFile];
}
I think your issue may be that you're testing Documents/file1.txt and not /Documents/file1.txt
The leading '/' is important
[edit]
May I make a suggestion? Distill this down to what works first and then figure out what makes it fail?
I would recommend using the following form and continuing from there:
if ([fileManager isWritableFileAtPath: #"/Documents/file1.txt"] == YES)
NSLog (#"File is writable");
else
NSLog (#"File is read only");
[/edit]