UIImage這個對象是swift中的圖像類,可以使用UIImageView加載顯示到View上。
以下是UIImage的構造函數:
init(named name: String!) -> UIImage // load from main bundle init(named name: String!, inBundle bundle: NSBundle!, compatibleWithTraitCollection traitCollection: UITraitCollection!) -> UIImage init(contentsOfFile path: String!) init(data: NSData!) init(data: NSData!, scale: CGFloat) init(CGImage cgImage: CGImage!) init(CGImage cgImage: CGImage!, scale: CGFloat, orientation: UIImageOrientation) init(CIImage ciImage: CIImage!) init(CIImage ciImage: CIImage!, scale: CGFloat, orientation: UIImageOrientation)
第一種,可以直接指定圖片的名稱來加載圖像,但這種圖片必須是以資源文件形式才可以正常加載。
var img = UIImage(named:"04") //初始化圖片 var vImg = UIImageView(image: img); //初始化圖片View vImg.frame.origin = CGPoint(x:0,y:20); //指定圖片顯示的位置 //vImg.frame = CGRect(x:0,y:20,width:120,height:120); //指定圖片的位置以及顯示的大小 self.view.addSubview(vImg); //顯示在View上
第二種,可以使用data來構造函數,參數data須要是NSData對象。相比較來說,NSData更加靈活,你可以使用NSData加載本地文件,也可以加載網絡地址文件。
NSData是什么呢?我的理解就是適合網絡傳輸的二進制數據(或者相類似的理解)
init(contentsOfFile path: String!) init(contentsOfURL url: NSURL!)
這個是NSData其中的2個構造函數, 通過這2個構造函數,我們可以指定文件本地路徑或者網絡路徑來將圖像轉換為NSData對象。我想指定本地路徑的方法,可能在由用戶指定背景圖片(頭像)或者背景音樂之類的情況下比較合適。
var nsd = NSData(contentsOfURL:NSURL.URLWithString("http://ww2.sinaimg.cn/bmiddle/632dab64jw1ehgcjf2rd5j20ak07w767.jpg")) var img = UIImage(data: nsd); var vImg = UIImageView(image: img); vImg.frame.origin = CGPoint(x:0,y:20); //vImg.frame = CGRect(x:0,y:20,width:120,height:120); self.view.addSubview(vImg);
在使用UIImageView對象加載並顯示到View時,有時候我們僅需指定圖像的xy點即可,如果同時指定了width和height的話,圖像顯示會變形的。所以在這里,我只指定了frame的origin(CGPoint),而沒有指定CGSize. 但有時候顯示出來的圖片雖然沒有變形,但會太大,或者太小,同樣對視覺造成影響。這個時候,可以在創建UIImage對象的時候,指定scale來對圖像進行縮放來適應設備size。但這個參數我嘗試了下,發現和我的理解還是有些偏差的。如果=1,是正常大小,如果小於1,比如0.5,會發現圖片基本是原來尺寸的2倍,反而設置為1.5,或者2,數據越大,比例越小。
var nsd = NSData(contentsOfURL:NSURL.URLWithString("http://ww2.sinaimg.cn/bmiddle/632dab64jw1ehgcjf2rd5j20ak07w767.jpg")) var img = UIImage(data: nsd,scale:1.5); //在這里對圖片顯示進行比例縮放 var vImg = UIImageView(image: img); vImg.frame.origin = CGPoint(x:0,y:20); //vImg.frame = CGRect(x:0,y:20,width:120,height:120); self.view.addSubview(vImg);
以下是官方對scale參數的解釋:
The scale factor to assume when interpreting the image data. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size
property.
總結:
1)如果需要顯示app自帶的資源文件,可以直接使用UIImage的構造函數:named來直接拉取資源文件。
2)如果要顯示本地或者網絡資源文件,則需要使用NSData,來拉取對應文件的DATA,最后顯示到UIImageView中去。
3)UIImage在構造時,scale的參數需要進一步理解。
4)如果文件尺寸未知的情況下,最好不要對其width和height進行限制。可使用UIView.frame.origin(CGPoint)來指定左上角坐標。同樣,也可以單獨指定UIView.frame.size來指定CGSize.
5)如果僅指定圖像左上角坐標,但又想文件按比例縮放,可以使用vImg的contentMode屬性枚舉值
var nsd = NSData(contentsOfURL:NSURL.URLWithString("http://ww2.sinaimg.cn/bmiddle/632dab64jw1ehgcjf2rd5j20ak07w767.jpg")) var img = UIImage(data: nsd,scale:1.5); var vImg = UIImageView(image: img); vImg.frame.origin = CGPoint(x:0,y:20); //vImg.frame.size.height = 100//self.view.bounds.width; //vImg.frame = CGRect(x:0,y:20,width:120,height:120); vImg.contentMode = UIViewContentMode.ScaleAspectFit; self.view.addSubview(vImg);
enum UIViewContentMode : Int { case ScaleToFill case ScaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent case ScaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped. case Redraw // redraw on bounds change (calls -setNeedsDisplay) case Center // contents remain same size. positioned adjusted. case Top case Bottom case Left case Right case TopLeft case TopRight case BottomLeft case BottomRight }
最后,如果需要對加載的圖像進行圓角矩形處理,可以對UIImageView的Layer屬性設置
vImg.layer.cornerRadius = 8; vImg.layer.masksToBounds = true;