提取使用URL的圖像我有一個UIView,我試圖從URL獲取一些數據,如以下,如何在iOS的
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat: @"www.url";
// Create a NSURLRequest with the given URL
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:30.0];
[request setValue:@"03f0d561-e173-4296-ab07-0ba301ca56bb" forHTTPHeaderField:@"token"];
// Get the data
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// Now create a NSDictionary from the JSON data
if (data != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
jsonDataDictionary = [[jsonDictionary objectForKey:@"data"] objectAtIndex:0];
});
}
});
,當我運行此代碼,圖像是黑色的。圖片沒有加載。誰能幫我這個。
在此先感謝!
請問您的網址包含方案(例如,「HTTP://」)?這是必要的。記錄'NSURL'並確保它不是'nil'。另外,你說它不工作,但是你沒有使用NSURLConnection或NSJSONSerialization中的任何'NSError'參數來告訴你爲什麼失敗。你應該使用那些'NSError'參數,如果失敗就記錄它們。否則,你是盲目的。 – Rob 2014-10-28 03:33:44