我正在尝试从 Twitter 加载图片。如果我只使用 json 结果中的 URL 而不进行编码,则在 中dataWithContentsOfURL
,我得到 nil URL 参数。如果我对其进行编码,我得到如下
%0A%20%20%20%20%22http://example.com/example.jpg%22%0A。
我知道我可以使用rangeOfString:
,或者stringByReplacingOccurrencesOfString:
但我可以确定它总是一样的,有没有另一种方法来处理这个问题,为什么这发生在我的 Twitter 响应而不是我的 instagram 响应中?
我也试过
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
它什么也不做。
这是直接来自json的URL ...
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;
});
});
}
}