{"id":584,"date":"2023-02-25T19:40:00","date_gmt":"2023-02-25T11:40:00","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=584"},"modified":"2023-04-29T20:31:16","modified_gmt":"2023-04-29T12:31:16","slug":"swift-ui-image-control-uiimageview","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-image-control-uiimageview\/","title":{"rendered":"Swift UI &#8211; \u56fe\u50cf\u63a7\u4ef6\uff08UIImageView\uff09"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h3>\u4f7f\u7528\u56fe\u50cf\u63a7\u4ef6\u663e\u793a\u56fe\u7247<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-swift\">let imageView = UIImageView(image:UIImage(named:&quot;image1&quot;))\nimageView.frame = CGRect(x:10, y:30, width:300, height:150)\nself.view.addSubview(imageView)<\/code><\/pre>\n<h3>\u6539\u53d8\u56fe\u7247<\/h3>\n<pre><code class=\"language-swift\">imageView.image = UIImage(named:&quot;icon2&quot;)<\/code><\/pre>\n<h3>\u4ece\u6587\u4ef6\u76ee\u5f55\u4e2d\u83b7\u53d6\u56fe\u7247<\/h3>\n<pre><code class=\"language-swift\">let path = Bundle.main.path(forResource: &quot;ball&quot;, ofType: &quot;png&quot;)\nlet newImage = UIImage(contentsOfFile: path!)\nlet imageView = UIImageView(image:newImage)\nself.view.addSubview(imageView)<\/code><\/pre>\n<h3>\u4ece\u7f51\u7edc\u5730\u5740\u83b7\u53d6\u56fe\u7247<\/h3>\n<pre><code class=\"language-swift\">\/\/\u5b9a\u4e49URL\u5bf9\u8c61\nlet url = URL(string: &quot;http:\/\/www.appblog.cn\/css\/images\/logo128.jpg&quot;)\n\/\/\u4ece\u7f51\u7edc\u83b7\u53d6\u6570\u636e\u6d41\nlet data = try! Data(contentsOf: url!)\n\/\/\u901a\u8fc7\u6570\u636e\u6d41\u521d\u59cb\u5316\u56fe\u7247\nlet newImage = UIImage(data: data)\nlet imageView = UIImageView(image:newImage);\nself.view.addSubview(imageView)<\/code><\/pre>\n<h3>\u4f7f\u7528\u56fe\u50cf\u63a7\u4ef6\u5b9e\u73b0\u52a8\u753b\u64ad\u653e<\/h3>\n<p><code>UIImageView<\/code> \u4e2d\u63d0\u4f9b\u4e86\u5b58\u50a8\u591a\u5f20\u56fe\u7247\u6765\u521b\u5efa\u52a8\u753b\u7684\u529f\u80fd\uff0c\u5177\u4f53\u505a\u6cd5\u662f\uff0c\u5728 <code>animationImages<\/code> \u5c5e\u6027\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u56fe\u7247\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 <code>startAnimating<\/code> \u65b9\u6cd5\u5f00\u59cb\u52a8\u753b\uff0c\u6700\u540e\u7528 <code>stopAnimating<\/code> \u65b9\u6cd5\u505c\u6b62\u52a8\u753b\u3002\u540c\u65f6\uff0c\u4f7f\u7528 <code>animationDuration<\/code> \u5c5e\u6027\u4e2d\u53ef\u4ee5\u8bbe\u7f6e\u52a8\u753b\u6bcf\u5e27\u5207\u6362\u7684\u901f\u5ea6\uff08\u79d2\uff09\u3002<\/p>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController {\n\n    var imageView:UIImageView!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        imageView = UIImageView()\n        imageView.frame = CGRect(x:20, y:20, width:100, height:100)\n        \/\/\u8bbe\u7f6e\u52a8\u753b\u56fe\u7247\n        imageView.animationImages = [UIImage(named:&quot;icon1&quot;)!, UIImage(named:&quot;icon2&quot;)!]\n        \/\/\u8bbe\u7f6e\u6bcf\u96940.5\u79d2\u53d8\u5316\u4e00\u6b21\n        imageView.animationDuration = 0.5\n        self.view.addSubview(imageView)\n    }\n\n    override func viewWillAppear(_ animated: Bool) {\n        super.viewWillAppear(animated)\n        imageView.startAnimating()\n    }\n\n    override func viewWillDisappear(_ animated: Bool) {\n        super.viewWillAppear(animated)\n        imageView.stopAnimating()\n    }\n\n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/code><\/pre>\n<h3>\u4fdd\u6301\u56fe\u7247\u6bd4\u4f8b<\/h3>\n<p>\u9ed8\u8ba4 <code>UIImageView<\/code> \u4f1a\u62c9\u4f38\u56fe\u7247\u4f7f\u5176\u5360\u6ee1\u6574\u4e2a <code>UIImageView<\/code>\uff0c\u5982\u679c\u4e0d\u60f3\u8ba9\u56fe\u7247\u53d8\u5f62\uff0c\u53ef\u4ee5\u5c06 <code>ContentMode<\/code> \u8bbe\u7f6e\u4e3a <code>Aspect Fit<\/code>\u3002<\/p>\n<pre><code class=\"language-swift\">imageView.contentMode = .scaleAspectFit<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u4f7f\u7528\u56fe\u50cf\u63a7\u4ef6\u663e\u793a\u56fe\u7247 let imageView = UIImageView(i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}