NSUTF8StringEncoding gives me this %0A%20%20%20%20%22http://example.com/example.jpg%22%0A

谁说我不能喝 提交于 2019-12-25 01:49:39

问题


I'm trying to load pictures from twitter. If i just use the URL in the json results without encoding, in the dataWithContentsOfURL, I get nil URL argument. If I encode it, I get as follow's

%0A%20%20%20%20%22http://example.com/example.jpg%22%0A.

I know I can use rangeOfString: or stringByReplacingOccurrencesOfString: but can I be sure that it will always be the same, is there another way to handle this, and why is this happening to my twitter response and not my instagram response?

I have also tried

stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]

and it does nothing.

This is the URL directly from the json...

2013-11-08 22:09:31:812 JaVu[1839:1547] -[SingleEventTableViewController tableView:cellForRowAtIndexPath:] [Line 406] (
    "http://pbs.twimg.com/media/BYWHiq1IYAAwSCR.jpg"
)

Here is my code

if ([post valueForKeyPath:@"entities.media.media_url"]) {
        NSString *twitterString = [[NSString stringWithFormat:@"%@", [post valueForKeyPath:@"entities.media.media_url"]]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        twitterString = [twitterString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@", twitterString);

        if (twitterString != nil){
            NSURL *twitterPhotoUrl = [NSURL URLWithString:twitterString];
            NSLog(@"%@", twitterPhotoUrl);
            dispatch_queue_t queue = kBgQueue;
            dispatch_async(queue, ^{
                NSError *error;
                NSData* data = [NSData dataWithContentsOfURL:twitterPhotoUrl options:NSDataReadingUncached error:&error];
                UIImage *image = [UIImage imageWithData:data];

                dispatch_sync(dispatch_get_main_queue(), ^{
                    [streamPhotoArray replaceObjectAtIndex:indexPath.row withObject:image];
                    cell.instagramPhoto.image = image;
                });
            });
        }
    }

回答1:


The log output you show appears to be an array, since it has parentheses with the string on a separate line.

You shouldn't need to encode the string if it's already a valid URL string and in fact doing so is wrong and will break things.




回答2:


%0A%20%20%20%20%22 stands for a \n followed by 4 spaces and then ". This is in accordance with the description in the console. Have you used NSJSONSerialization to obtain the URL from JSON data?




回答3:


Not the answer rather a step in solving the error,: Change your code to this and post the output. Combining several method calls in one makes debugging harder.

NSLog(@"post: '%@", post);
NSString * twitterString;
twitterString = [post valueForKeyPath:@"entities.media.media_url"];
NSLog(@"twitterString 1: '%@", twitterString);
twitterString = [twitterString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"twitterString 2: '%@", twitterString);
        twitterString = [twitterString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"twitterString 3: '%@", twitterString);

BTW, [NSString stringWithFormat:@"%@"... is not necessary.



来源:https://stackoverflow.com/questions/19872010/nsutf8stringencoding-gives-me-this-0a2020202022http-example-com-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!