我試圖從互聯網下載圖像。我不確定爲什麼此代碼無法正常工作,但我想要使用這些規格(209x209像素,不超過65KB)獲取照片。將互聯網上的圖像保存到iPhone
寫入iPhone目錄:
NSData *imgAsData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//UIImage *image = [UIImage imageWithData:[NSData thumnail_url]];
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:self.product.img_url];
[myFileHandle writeData:UIImagePNGRepresentation(image)];
[myFileHandle closeFile];
我甚至嘗試將writeToFile,但它仍然無法正常工作。
[imgAsData writeToURL: [NSURL URLWithString:self.product.img_url] atomically:YES];
從iPhone目錄閱讀:
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:self.product.img_url];
//NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:self.product.thumbnail_url];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
我用的是在相同的應用程序的代碼下載縮略圖和他們的工作(注意使用「thumbnail_url」註釋掉行)。我試圖用這個「更大」的圖像來代替縮略圖網址,它工作。我可以離線保存縮略圖,但我需要獲得更大的版本(當然,因爲縮略圖太像素化了)。
我敢肯定,因爲我得到thumnails工作的路徑是好的。
我迷路了。這是不工作,因爲文件大小是「更大」?請幫助,謝謝!
你確定通往較大圖片的路徑可以嗎?你可以NSLog一個路徑並將其複製粘貼到瀏覽器中以查看圖像是否出現? – 2012-03-05 09:53:44
@camden_kid當我粘貼網址時它會出現在瀏覽器中的縮略圖圖像。但不適用於較大的圖像... – acecapades 2012-03-06 01:54:21
這告訴我,較大圖像的URL可能不正確。 – 2012-03-06 11:15:47