NSInvalidArgumentException', reason: '-[__NSCFString isFileURL]: unrecognized selector sent to instance 0x712e450' - objective-c

I am new to iPhone App development.
When I run a sample project, I did which parses an xml feed and displays the contents along with image in a table view, I get this error -
"NSInvalidArgumentException', reason: '-[__NSCFString isFileURL]: unrecognized selector sent to instance 0x712e450'"
It is occurring only when I try to display the image in UITableViewCell.
The code I used for getting images from url is -
if([elementName isEqualToString:IMAGE])
{
NSURL *imageUrl = [attributeDict objectForKey:#"url"];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
bbc.image = [UIImage imageWithData:imageData];
}
where bbc is a class(NSObject subclass) object used to store the parsed contents.

I think you are using NSString as NSURL. Try this:
NSURL *imageUrl =[NSURL URLWithString:[attributeDict objectForKey:#"url"]];

It looks like "url" is in fact an NSString, not an NSURL object. Convert it to an NSURL object yourself:
if ([elementName isEqualToString:IMAGE])
{
NSString *urlStr = [attributeDict objectForKey:#"url"];
NSURL *imageUrl = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
bbc.image = [UIImage imageWithData:imageData];
}

imageURL is not a NSURL, but a string.

Related

Unable to fetch data from nsurl

NSURL *url = [NSURL URLWithString:[self.imageArray objectAtIndex:indexPath.row]];
NSLog(#"My URL : %#",url);
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(#"New Data : %#",data);
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
cell.imageView.image = tmpImage;
i think i got your problem, check this like my upload image in your project.
i also make a gif about how to add this setting in your project , just follow the step in my gif.

Download ePub (NSURLConnection), save it, and open it with UIDocumentInteractionController

So I am trying to download an ePub book with NSURLConnection. So far, the download is working perfectly. The issue comes when I try to save it and open it. When I try to open it with UIDocumentInteractionController, the app crashes saying that the directory is null.
Here is my code:
//Download ePub file
NSURL *url = [NSURL URLWithString:#"http://www.terceiroanjo.com/materiais/missaopiloto.epub"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
then
// save it ("data" = NSMutableData that was appended from the received data of the download)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *filePath = [NSString stringWithFormat:#"%u/%#", NSDocumentDirectory,#"missaopiloto.epub"];
[data writeToFile:filePath atomically:YES];
}
and to open the ePub:
NSString *path = [[NSBundle mainBundle] pathForResource:#"missaopiloto" ofType:#"epub"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
When I try to open it, the app crashes:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
I get it, the directory is nill, but why? What am I doing wrong. Any ideas?
Thanks.
I think the problem here is that, once you download and save a file, it's not becoming part of the bundle.
If you want to access the file you must provide the local path and create the local URL
NSString *path = [NSString stringWithFormat:#"%u/%#", NSDocumentDirectory,#"missaopiloto.epub"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

URL with special character in Objective-c

I'm trying to use this URL in my application so that, when the user provides the right info he will be able to log in:
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:#"https://www.example.come/login/CheckEligibility?json={'username'%%3A'%#'%%2C'password'%%3A'%#'}", username.text,passowrd.text]]];
For some reason I'm getting the exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
and when I test the URL in the browser, it works fine and gives me the data I want.
Try this
NSString *str = [NSString stringWithFormat:#"https://www.example.come/login/CheckEligibility?json={'username':'%#','password':'%#'}", username.text, passowrd.text];
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];
You need to encode the string like:
[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Setting Image of TableView Cell NSURL isResizable error

I am using some regex expression to get the first URL in an xml which is a link to a photo, and set that photo as the cell's imageview image. Here is my code:
NSString *thearticleImage = entry.articleImage;
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:#"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];
NSString *someString = thearticleImage;
self.theurl = [someString substringWithRange:[expression rangeOfFirstMatchInString:someString options:NSMatchingCompleted range:NSMakeRange(0, [someString length])]];
cell.imageView.image = [NSURL URLWithString:theurl];
I end up gettin the error:
'-[NSURL _isResizable]: unrecognized selector sent to instance 0x882e250'
What is the issue with this?
you can't use url to imageView.image so you can use this like below
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"your image link"]];
UIImage *downloadedImage = [UIImage imageWithData:imageData];
cell.imageView.image = downloadedImage;
But it will produce crash...
NOTE
To overcome with this crash you must hit the URL asynchronously .BY this it will not produce crash and table will freely scroll

Objective-C how to get an image from a url

Am having some difficulty getting an Image from a url, and then displaying it in an image well in the interface.
This is the code I'm currently using, it doesn't work and compiles with no errors:
NSURL *url = [NSURL URLWithString:#"http://www.nataliedee.com/061105/hey-whale.jpg"];
NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[imageView setImage:[NSImage imageNamed:newIMAGE]];
Any ideas as to what is wrong here?
You are passing in image data as a string to a method that is trying to use that string as the name of the image, which of course doesn't exist.
What you need to do is create an NSData object from your URL dataWithContentsOfURL:, once you have the NSData object use this to create the UIImage via imageWithData:.