NSURL may not respond warning +encryptedFileWithPath - ios

I am using the following code which is coming up with a warning. The code does display the correct image - but how can I get rid of the warning?
NSString *indexPath = [[NSBundle mainBundle] pathForResource:name ofType:#"png" inDirectory:#"tunes"];
NSURL *url = [NSURL encryptedFileURLWithPath:indexPath];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
warning 'NSURL may not respond to +encryptedFileWithPath:'

That is because NSURL does not have a method called `encryptedFileWithPath:'. If you have copied your code from here, you probably didn't read the article carefully enough:
If you are familiar with NSURL and its class methods then you may have
spotted the unfamiliar encryptedFileURLWithPath: method. I have
extended NSURL using a category to add this method as a convenience.

Related

Unable to create NSURL with special characters (éè) with Objective C

I am unable to load an image because of nil returned by NSURL. The url looks like this : http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg. When I enter my URL on firefox, I get my image but when I try in firefox the encoder http://www.example.fr/wp-content/uploads/2014/06/pr%3Flinaire.jpg I have no image...
Here is the code I use to load my image :
NSString *myString = #"http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg";
NSURL *url = [NSURL URLWithString:myString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
That returns nil for url, so everything after is nil..
Thank you for your help guys
That's because those characters are not valid in a URL. Escape them first:
NSString *escaped = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escaped];
// etc.

Getting an image fails to create NSURL

Im trying to get an image from a url link.
NSURL *thmbnailUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]];
NSLog(#"thmbnail:%#",[NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]);
NSData *data = [NSData dataWithContentsOfURL:thmbnailUrl];
NSLog(#"movimgdata:%#",data);
UIImage *img = [[UIImage alloc]initWithData:data];
NSLog(#"movimg:%#",data);
NSData *imgData = UIImagePNGRepresentation(img);
when i log thmbnail i get this "ipofserverinnetwork/fishtune/movie/Our Logo 480p back.png"
Note:i didn't put the original ip.
I get null when i log data and of course img will also be null. I really dont know why I'm getting null. because when I tried entering the url in a browser it displays the image. I used the same code in another part of my app and it works but when i used it in this it fails. Is it because of the spaces between "Our_Logo_480p"?
you might need to use
NSString* escapedUrl = [yourstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
yes it's because of spaces, use
NSString *myUrl = [NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]];
NSString *url = [myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

ImageView loads local image but not from URL

I am having trouble getting an image to load from a URL. I have CollectionViewCell subclass with an image view setup like this:
#property (strong, nonatomic) IBOutlet UIImageView *albumCover;
I cannot get it to load images from a URL. Here is the code im using:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: song.albumURL]];
cell.albumCover = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];
Meanwhile, this works:
cell.albumCover.image = [UIImage imageNamed:#"lock.png"];
I know the URL connection is getting the proper image back because I am monitoring it with Fiddler. Why isn't the image showing up then?
I was allocating the UIImageView object again instead of just initializing it with the image. The correct code is:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: song.albumURL]];
[cell.albumCover initWithImage:[UIImage imageWithData: imageData]];
I use SDWebImageView:
Asynchronous image downloader with cache support with an UIImageView category
http://hackemist.com/SDWebImage/doc
You should use fileURLWithPath: instead of the URLWithString:.
URLWithString: is for WWW URL.
And an example:
NSString *rootPath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [rootPath stringByAppendingPathComponent:#"example.mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
yourMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];

NSData dataWithContentsOfURL: returning nil

NSString *jsonPath = [[NSBundle mainBundle] pathForResource:#"CRN_JSON"
ofType:#"json"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
data = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://properfrattire.com/Classifi/CRN_JSON.json"]];
[self performSelectorOnMainThread:#selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
My data variable is nil after running this code. If you follow the link you'll see that it is a JSON file. I've run the function with this exact same file locally but it is not able to obtain the data at the given URL without error.
Not sure why you're nesting calls to URLWithString:
[NSURL URLWithString:[NSURL URLWithString:#"http://properfrattire.com/Classifi/CRN_JSON.json"]]];
Once will do:
[NSURL URLWithString:#"http://properfrattire.com/Classifi/CRN_JSON.json"];
Also, you should use dataWithContentsOfURL:options:error: so you can see any error.

NSURL with Pipes |

I'm trying to initialize an UIImage via URL.
This is the code I wrote:
NSString * path = [NSString stringWithFormat:#"%#",[[_tours objectAtIndex:indexPath.row]objectForKey:#"map_url"]];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
[[cell tourPreviewImage]setImage:img];
....
An example map_url looks like the following:
https://maps.google.com/maps/api/staticmap?size=200x200&maptype=roadmap&markers=size:big|color:green|72.3601030185,41.4626763775&markers=size:big|color:red|45.4563468516,94.4774796973&sensor=false
It seems that the NSURL Object cant handle the | Pipe Character the right way because i dont get any image data. If I delete the parameters separated by the pipe out of the url everything works fine.
Any Ideas how to solve this problem?
ok i found a solution for this. Encoding is what did go wrong.
NSString * path = [NSString stringWithFormat:#"%#",[[_tours objectAtIndex:indexPath.row]objectForKey:#"map_url"]];
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
[[cell tourPreviewImage]setImage:img];

Resources