问题描述
我正在尝试从Twitter加载图片.如果我只是在json结果中使用URL而不进行编码,则在dataWithContentsOfURL
中,我会得到nil URL参数.如果我对它进行编码,我将得到如下的
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
我知道我可以使用rangeOfString:
或stringByReplacingOccurrencesOfString:
,但是我可以确定它始终是相同的,是否有另一种方法可以处理此问题,为什么这是发生在我的Twitter响应而不是我的instagram响应上?
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?
我也尝试过
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
它什么都不做.
这是直接来自json的URL ...
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"
)
这是我的代码
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;
});
});
}
}
推荐答案
您显示的日志输出似乎是一个数组,因为它的括号中带有单独的一行的字符串.
The log output you show appears to be an array, since it has parentheses with the string on a separate line.
如果该字符串已经是有效的URL字符串,则不需要编码,而实际上这样做是错误的,并且会破坏内容.
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.
这篇关于NSUTF8StringEncoding给了我%0A%20%20%20%20%22http://example.com/example.jpg%22%0A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!