Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSData & NSURL - url with space having problem

I have following code in my application.

NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]]; 

pathOfThumbNail has following path


http://70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg


When I open above path in safari browser - path is changed automatically & image is successfully displayed.

http://70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg


But in iPhone, due to space in path, image isn't loaded in nsdata.

like image 684
Sagar Kothari Avatar asked Sep 17 '09 20:09

Sagar Kothari


People also ask

What is NSData?

NSData provides methods for atomically saving their contents to a file, which guarantee that the data is either saved in its entirety, or it fails completely. An atomic write first writes the data to a temporary file and then, only if this write succeeds, moves the temporary file to its final location.

What is data () in Swift?

Data in Swift 3 is a struct that conforms to collection protocol. It is a collection of bytes ( [UInt8] array of unsigned integer 8 bits 0-255).

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

What is Nsurl?

An NSURL object is composed of two parts—a potentially nil base URL and a string that is resolved relative to the base URL. An NSURL object is considered absolute if its string part is fully resolved without a base; all other URLs are considered relative.


1 Answers

Use: stringByAddingPercentEscapesUsingEncoding:

Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding 

A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding cannot encode a particular character

Added per request by @rule

NSString* urlText = @"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg"; NSString* urlTextEscaped = [urlText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString: urlTextEscaped]; NSLog(@"urlText:        '%@'", urlText); NSLog(@"urlTextEscaped: '%@'", urlTextEscaped); NSLog(@"url:            '%@'", url); 

NSLog output:

 urlText:        '70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg'   urlTextEscaped: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'   url:            '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'   
like image 65
zaph Avatar answered Oct 26 '22 10:10

zaph