iOS 網絡與多線程--6.下載並保存網絡圖片

使用二進制數據對象的,從制定網站獲取數據的方法,下載網絡圖片,並轉化爲二進制數據,而後將二進制數據保存到磁盤網絡

按照註釋須要進行閱讀如下代碼iphone

 1 //  Created by JinXin on 15/12/2.
 2 //  Copyright © 2015年 JinXin. All rights reserved.
 3 //
 4 
 5 #import "ViewController.h"
 6 
 7 @interface ViewController ()
 8 @end
 9 
10 @implementation ViewController
11 
12 - (void)viewDidLoad {
13     [super viewDidLoad];
14     // Do any additional setup after loading the view, typically from a nib.
15     
16     // 5.獲取項目沙箱中的Documents文件夾。
17     NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
18             NSUserDomainMask, YES) objectAtIndex:0];
19     // 6.獲取網絡圖片
20     UIImage *imageFromURL = [self getImageFromURL:@"http://face.weiphone.net/data/avatar/001/27/35/59_avatar_big.jpg"];
21     // 7.將圖片保存到磁盤
22     [self saveImage:imageFromURL withFileNmae:@"image" ofType:@"jpg" inDirectory:documentsDirectoryPath];
23     // 7.輸出路徑
24     NSLog(@"Path of saved image:%@",documentsDirectoryPath);
25     
26 }
27 
28 // 1.建立一個方法,用來獲取網絡圖片,參數爲圖片的網絡路徑
29 -(UIImage *)getImageFromURL:(NSString *)fileURL
30 {
31     NSLog(@"Getting image........");
32     UIImage *result;
33     
34     // 2.獲取網絡圖片,並將數據存入二進制數據對象
35     NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
36     // 3.將二進制數據轉爲圖像
37     result = [UIImage imageWithData:data];
38     // 4.返回結果
39     return result;
40 }
41 
42 // 2.建立一個方法,用來將圖像保存到磁盤
43 // 參數1.UIImage 對象
44 // 參數2.文件名
45 // 參數3.文件類型
46 // 參數4.保存文件的路徑
47 -(void)saveImage:(UIImage *)image withFileNmae:(NSString *)imageName ofType:(NSString *)extension
48      inDirectory:(NSString *)directoryPath
49 {
50     // 3.檢測圖片擴展名,若是是png後綴,則使用UIImagePNGRepresentation方法,將圖片轉爲二進制數據,並寫入磁盤
51     if ([[extension lowercaseString] isEqualToString:@"png"])
52     {
53         [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:
54             [NSString stringWithFormat:@"%@.%@",imageName,@"png"]] options:NSAtomicWrite error:nil];
55     }
56     // 4. 檢測圖片擴展名,若是是jpg後綴,則使用UIImageJPEGRepresentation方法,將圖片轉爲二進制數據,並寫入磁盤
57     else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"] )
58     {
59         [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:
60             [NSString stringWithFormat:@"%@.%@",imageName,@"jpg"]] options:NSAtomicWrite error:nil];
61     }
62 }
63 
64 - (void)didReceiveMemoryWarning {
65     [super didReceiveMemoryWarning];
66     // Dispose of any resources that can be recreated.
67 }
68 
69 @end

輸出結果:網站

1 2015-12-04 21:58:47.132 AppDemo[1030:104437] Getting image........
2 2015-12-04 21:58:47.312 AppDemo[1030:104437] Path of saved image:/Users/jinxin/Library/Developer/CoreSimulator/Devices/78B0547C-FAC7-4343-AAF0-EEB564932491/data/Containers/Data/Application/5051F20E-5736-46DC-BC4E-C00D9F9CDDFF/Documents

以後打開finder 按Shift+Command+G 將路徑粘貼進去 進入目錄spa

將能夠看到下載到的圖片了.net

相關文章
相關標籤/搜索