惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
博客园 - 司徒正美
L
LangChain Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
Project Zero
Project Zero
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Troy Hunt's Blog
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Scott Helme
Scott Helme
Recent Announcements
Recent Announcements
S
Secure Thoughts
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
Hacker News - Newest:
Hacker News - Newest: "LLM"
雷峰网
雷峰网
Attack and Defense Labs
Attack and Defense Labs
A
About on SuperTechFans
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
Martin Fowler
Martin Fowler
V2EX - 技术
V2EX - 技术
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
B
Blog RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
PCI Perspectives
PCI Perspectives
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hacker News: Front Page

博客园 - 房客

用JavaDoc生成项目文档 thymeleaf参考手册 转的一个Java基本功 杂记 修改Esxi克隆的CentOS的IP地址 CentOS搭建socket5代理服务器 CentOS上搭建Nginx + Mono 运行 asp.net 启动PPT的时候一直配置vs2013的问题解决 swift 元组 swift 集合类型(二) swift 集合类型(一) swift 定时器的使用 swift 随机生成背景颜色 VSFTPD配置TLS/SSL JayProxy的设置 CentOS 安装配置 PPTP VPN 服务器 - 房客 Nginx + fastcgi 处理php 编译安装php5.3.8(含php-fpm) CentOS 安装eaccelerator PHP加速
swift UIImage加载远程图片和圆角矩形
房客 · 2014-06-17 · via 博客园 - 房客

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;