亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

iOS:從網址下載圖片并保存在設備中

iOS:從網址下載圖片并保存在設備中

largeQ 2020-02-04 14:43:04
我正在嘗試從網址http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg下載圖像我正在使用以下代碼,但是圖像未保存在設備中。我想知道我在做什么錯。 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg"]]; [NSURLConnection connectionWithRequest:request delegate:self]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"pkm.jpg"]; NSData *thedata = NULL; [thedata writeToFile:localFilePath atomically:YES]; UIImage *img = [[UIImage alloc] initWithData:thedata];
查看完整描述

3 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

如果設置theDatanil,您希望它將什么寫入磁盤?

您可以使用的是NSData* theData = [NSData dataWithContentsOfURL:yourURLHere];從磁盤加載數據,然后使用進行保存writeToFile:atomically:。如果您需要對加載過程進行更多控制或將其置于后臺,請查看的文檔NSURLConnection以及相關的指南。


查看完整回答
反對 回復 2020-02-04
  • 3 回答
  • 0 關注
  • 1015 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號