swift 怎么用像素填充率UIImage

当前位置:
& Swift - 图像控件(UIImageView)的用法
Swift - 图像控件(UIImageView)的用法
发布:hangge
阅读:12952
(本文代码已升级至Swift3)
1,使用图像控件显示图片
let imageView = UIImageView(image:UIImage(named:"image1"))
imageView.frame = CGRect(x:10, y:30, width:300, height:150)
self.view.addSubview(imageView)
2,改变图片
imageView.image = UIImage(named:"icon2")
3,从文件目录中获取图片
let path = Bundle.main.path(forResource: "ball", ofType: "png")
let newImage = UIImage(contentsOfFile: path!)
let imageView = UIImageView(image:newImage)
self.view.addSubview(imageView)
4,从网络地址获取图片
//定义URL对象
let url = URL(string: "/blog/images/logo.png")
//从网络获取数据流
let data = try! Data(contentsOf: url!)
//通过数据流初始化图片
let newImage = UIImage(data: data)
let imageView = UIImageView(image:newImage);
self.view.addSubview(imageView)
5,使用图像控件实现动画播放
UIImageView 中提供了存储多张图片来创建动画的功能,具体做法是,在 animationImages 属性中设置一个图片数组,然后使用 startAnimating 方法开始动画,最后用 stopAnimating 方法停止动画。同时,使用 animationDuration 属性中可以设置动画每帧切换的速度(秒)。
import UIKit
class ViewController: UIViewController {
var imageView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView()
imageView.frame=CGRect(x:20, y:20, width:100, height:100)
//设置动画图片
imageView.animationImages = [UIImage(named:"icon1")!,UIImage(named:"icon2")!]
//设置每隔0.5秒变化一次
imageView.animationDuration=0.5
self.view.addSubview(imageView)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
imageView.startAnimating()
override func viewWillDisappear(_ animated: Bool) {
super.viewWillAppear(animated)
imageView.stopAnimating()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
6,保持图片比例&
默认 UIImageView 会拉伸图片使其占满整个 UIImageView,如果不想让图片变形,可以将 ContentMode 设置为 Aspect Fit。
imageView.contentMode = .scaleAspectFit您的位置: &
& Swift - 如何让UIView,UILabel和UIImageView之间类型互相转化
和上一篇博文一样,这个问题同样出在自定义tabbar里面,上一篇中判断是不是某类型拿到了UIView上放着的UIImageView和UILabel,要想对后者作修改肯定要变换类型,按照Object的尿性,网上各种变换,不一一列出,都没用,后来博主就在那里根据自己知道的知识来尝试,结果成了,看代码:
原Object-C代码:
UIImageView * imageView=(UIImageView *)[view.subviews objectAtIndex:0];
UILabel * label=(UILabel *)[view.subviews objectAtIndex:1];
这样的用法想必大家都不陌生,但是在Swift中怎么用呢,不要急,看下面:
let imageView = view.subviews[0] as! UIImageView
let label = view.subviews[1] as! UILabel
下面对于as?和as!来进行解释:
首先它们都是用来转换类型的
其次,as?提供了一种类似于容错的机制,如果可以转换成功就转换成后面对应的类型,否则为nil,as!的‘!’在前面我们知道有强制解析的意思,在这里也是强制转换,在运行中可能会出现错误而系统崩溃的情况,不过我们一般用都是可以确定类型的,如果崩溃,记得检查类型。
还有就是Swift中对类型的匹配非常严格,一定要谨慎使用。
2天前236阅2天前335阅3天前457阅3天前412阅4天前172阅
CentOS专题
1856阅3570阅1824阅5706阅6059阅7579阅4465阅3412阅9484阅1940阅
5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!
按 Ctrl+D 键,
把本文加入收藏夹
热门栏目 &
热门教程 &
图书推荐 &
12345678910
12345678910解决ios - Flipping [UIImage] set in Swift - uiimageview-uiimage-ios -
解决ios - Flipping [UIImage] set in Swift
相关推荐:ge using the old image as a source, with the new image being rotated in some way... say flipped vertically. This question was already answered a few m
I have two images that I want to switch between, lets call them fish and fisha. The UIImageView is called player, so the code is made as follows:
@IBOutlet weak var player: UIImageView!
var images: [UIImage] = []
images.append(UIImage(named: &fish&)!)
images.append(UIImage(named: &fisha&)!)
player.frame = CGRectMake((screenWidth-px)/2, (screenHeight-py)/2, px, py)
player.animationImages = images
player.animationDuration = 0.7
player.startAnimating()
This all works fine. But now say i want to make a second UIImageView called player2, which should be the mirrored version of player.
Using this function:
func flipH(im:UIImage)-&UIImage {
var newOrient:UIImageOrientation
switch im.imageOrientation {
newOrient = .UpMirrored
case .UpMirrored:
newOrient = .Up
case .Down:
newOrient = .DownMirrored
case .DownMirrored:
newOrient = .Down
case .Left:
newOrient = .RightMirrored
case .LeftMirrored:
newOrient = .Right
case .Right:
newOrient = .LeftMirrored
case .RightMirrored:
newOrient = .Left
return UIImage(CGImage: im.CGImage!, scale: im.scale, orientation: newOrient)
Along with this modification of the code to make player2 i expect it to work properly.
@IBOutlet weak var player2: UIImageView!
var images: [UIImage] = []
images.append(flipH(UIImage(named: &fish&)!))
images.append(flipH(UIImage(named: &fisha&)!))
player2.frame = CGRectMake((screenWidth-px)/2, (screenHeight-py)/2, px, py)
player2.animationImages = images
player2.animationDuration = 0.7
player2.startAnimating()
However, it doesnt. Player2 is identical to player. Does anyone have any idea why??
Thanks, Tom
171k 21 216 264 asked Mar 22 '16 at 18:44
1 Answers 1
-------Accepted-------Accepted-------Accepted-------
It's because the orientation of a UIImage doesn't mean what you think it means. It isn't some sort of magical image-transformer. If you want to flip an image, flip it (i.e. draw it in a graphics context with a flipped CTM).
For example, here's an image displayed in an image view:相关推荐:vedPhotosAlbum library. Once user takes a photo I save it in SavedPhotosAlbum and the following method is called: override func image(image: UIImage,
Here's the same image, flipped right-to-left, displayed in an image view:
The underlying code involves drawing the first image into a CTM-flipped graphics context to derive the second image.
answered Mar 22 '16 at 18:48
237k 33 375 520 && & Ah, is there no way to programmatically flip UIImages like I want? –&
Mar 22 '16 at 19:53 && & Yes there
is. It's just not what
you are doing. –&
Mar 22 '16 at 19:54
相关推荐:that when I hit the x, the UILabel disappears. I found this is one way to embed the image into the ui label: var attachment = NSTextAttachment()attac
相关阅读排行
相关内容推荐
请激活账号
为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。
您的注册邮箱:
如果您没有收到激活邮件,请注意检查垃圾箱。}

我要回帖

更多关于 ps中填充像素按钮 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信