iOS Seemingly the Same Strings with different behaviour - ios

I'm storing a WAV filename in an array, including the extenstion. When I try to load it it is requiring the extension be removed. If I substring the extension off it abends, while the seemingly same string works fine when hard-coded.
I also tried using the stringByDeletingPathExtension method, with the same crash.
This code causes a crash
NSString *fileName = [[currentWord.audioFile componentsSeparatedByString:#"."] objectAtIndex: 0];
SFCLog(#"fileName: %#",fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"wav"];
CFURLRef BaseURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
-[ViewController loadWord] [line 143] fileName: owe_fr
2012-08-20 17:50:21.561 Hello[8053:16a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
But this code executes
NSString *fileName = #"owe_fr";
SFCLog(#"fileName: %#",fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"wav"];
CFURLRef fluentBaseURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];

OK I found it. A leading space in the stored filename version - you can see it in the log statement. I found it by logging like this (Markers to either side of the string):
SFCLog(#"fileName :%#:",fileName);
Which logged as:
-[ViewController loadWord] [line 144] fileName : about_fr:
Cheers,
Sean

Related

SIGABRT on cast to UIImage

I'm using FastttCamera as a wrapper to AVFoundation for taking, processing and displaying a picture. Here's my code (a FastttCamera delegate method) in which I cast (FastttCapturedImage *) to (UIImage *):
- (void)cameraController:(FastttCamera *)cameraController didFinishScalingCapturedImage:(FastttCapturedImage *)capturedImage
{
//Use the captured image's data--note that the FastttCapturedImage is cast to a UIImage
NSData *pngData = UIImagePNGRepresentation((UIImage *)capturedImage);
//Save the image, and add the path to this transaction's picPath attribute
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
int timestamp = [[NSDate date] timeIntervalSince1970];
NSString *timeTag = [NSString stringWithFormat:#"%d",timestamp];
NSString *filePath = [documentsPath stringByAppendingString:timeTag]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
self.thisTransaction.picPath = filePath;
[self.viewFinder setImage:(UIImage *)capturedImage];
}
However, I'm getting a SIGABRT at the line:
NSData *pngData = UIImagePNGRepresentation((UIImage *)capturedImage);
with this console readout:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FastttCapturedImage CGImage]: unrecognized selector sent to instance 0x17e2a100'
I'm confused by the reference to CGImage. Is it a problem with my typecast? There was no complaint on build. Can someone please set me straight?
Read the docs for FastttCapturedImage. It's not a UIImage so you can't just cast it like that. Use the provided property to get the UIImage.
Change the problematic line to:
NSData *pngData = UIImagePNGRepresentation(capturedImage.fullImage);
You have the same problem later:
[self.viewFinder setImage:capturedImage.fullImage];
BTW - you didn't get a problem while compiling because a cast is a way to tell the compiler "trust me, it's really what I'm telling you it is". The problem here is you were wrong, it's not really what you claimed it was. :)

why does my app crash when I call for objectForInfoDictionaryKey: CFBundleShortVersionString

I am trying to get my version number of my app using:
NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: #"CFBundleShortVersionString"];
but it crashes with this error:
[NSString stringWithUTF8String:]: NULL cString
What am I doing wrong?
Check your info.plist to make sure short version (CFBundleShortVersionString) or (Bundle versions string, short) is set.
Your code is fine. But you can also call it this way:
NSString * version = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleShortVersionString"];

NSInvalidArgumentException with stringByAppendingPathComponent

i have a bundel settings with a string : version
in my bundle settings version = 2.9.1
in my project, if i use this code, all is good :
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"ma/V_2.9.1/gma2/fixture_layers/ifocus.xml"];
if i use this code :
NSString *path = [documentsDirectory stringByAppendingPathComponent:(#"ma/V_%#/gma2/fixture_layers/ifocus.xml", version)];
i have this error :
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextField length]: unrecognized selector sent to instance 0x9753190'
NSString * path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"ma/V_%#/gma2/fixture_layers/ifocus.xml", version]];
Since you're already including multiple /'s in your string, there's no advantage to using stringByAppendingPathComponent. So you may as well just use:
NSString* path = [documentsDirectory stringByAppendingFormat:#"ma/V_%#/gma2/fixture_layers/ifocus.xml", version];
Another option, that gives you the benefit of stringByAppendingPathComponent, is to break it right down like this:
NSString* path = [[[[[documentsDirectory stringByAppendingPathComponent:#"ma"] stringByAppendingPathComponent:[NSString stringWithFormat:#"V_%#", version]] stringByAppendingPathComponent:#"gma2"] stringByAppendingPathComponent:#"fixture_layers"] stringByAppendingPathComponent:#"ifocus.xml"];
But that's kind of ugly.

NSBundle pathForResource: returns nil

I am attempting to access a .txt file from my supporting files folder in Xcode on iOS using the following piece of code:
NSString* filePath = #"filename.txt";
NSLog(#"File path: %#", filePath);
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"txt"];
NSLog(#"File root: %#", fileRoot);
The first NSLog prints just what I expect it to print, but the last NSLog always simply prints
File root: (null)
Accessing text from the file after (attempting to) reading it into memory also simply gives me a (null) printout.
The solution for this problem is probably right under my nose, I just can't seem to find it. Any help is very appreciated! Thanks in advance.
filePath already contains the suffix ".txt", therefore you must not specify it
again when locating the resource. Either
NSString* filePath = #"filename.txt";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:nil];
or
NSString* filePath = #"filename";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"txt"];

UIWebview - Loading PNG image - got error

I am trying to load the image(png) in UIWebview. But I got the following error
""ImageIO: PNGinvalid literal/lengths set"
I will get the "png" from the web service. I will store it in the following path. "Users/XXX/Library/Application Support/iPhone Simulator/4.3.2/Applications/E4DE3097-EA84-492F-A2A7-5882202F3B00/Documents/attachement.png "
When I reading the file from the path, I got the error. I am using the following code
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *commentHtml = [NSString stringWithFormat:#"<img src=\"%#\"/>", documentEntity.path];
NSString *html3 = [NSString stringWithFormat:#"<html><head/><body><div><b>COMMENTS:</b></div>%#</body></html>", commentHtml];
[self.documentView loadHTMLString:html3 baseURL:baseURL];
Please help me.
THanks
Girija
If you have saved the Image in Documents Folder, why are you fetching from MainBundle.
Fetching Image from the Documents Directory would be :
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:#"%#/attachement.png", aDocumentsDirectory];
UIImage *anImage = [UIImage imageWithContentsOfFile:aFilePath];

Resources