UIImageView xib里面拉伸图片技巧
拉伸图片的时候代码里和xib里面的图片名字去掉@2x,但是原始图片文件得要xxx@2x.png
The X and Y values seem to be the positions for the stretch starting point, relative to the entire width and height of the image, i.e. 0.5 would mean a point in the middle of the image.
Same thing for the Width and Height: sizes for the stretchable area, relative to the image size, i.e. setting it to a value of 1 / imageWidth would mean the stretchable area is 1 pixel wide. The strange thing is 0 also work fine for the blue pill button (could
be for convenience reasons?).
Below, view modes are set to "Scale to Fill". The images are stretchable images fromhttps://github.com/0xced/UIKit-Artwork-Extractor.
This answer was inspired by http://www.slideshare.net/mystcolor/stretchable-images-in-uiimageview-
In cases where you want more than one pixel to stretch (ie. a pattern in the center of the button) the stretchable UIImage does not work.
The UIButton's contentStretch does also not work properly.
How I solved it: I subclassed UIButton and added a UIImageView
as property and placed it at index 0 as a subview within the
*backgroundImageViewUIButton
.
I then ensure in layoutSubviews that it fits the button entirely and set the highlighted states of the imageView. All you need to do is handover a UIImageView with the correct contentStretch and contentMode applied.
.h file
@interface ButtonWithBackgroundImage : UIButton {
UIImageView *backgroundImageView;
}
@property (retain) UIImageView *backgroundImageView;
+ (ButtonWithBackgroundImage*)button;
@end
.m file
@implementation ButtonWithBackgroundImage
@synthesize backgroundImageView;
+ (ButtonWithBackgroundImage*)button {
return [self buttonWithType:UIButtonTypeCustom];
}
- (void)setBackgroundImageView:(UIImageView*)img {
[backgroundImageView removeFromSuperview];
[backgroundImageView release];
backgroundImageView = [img retain];
if(backgroundImageView){
[self insertSubview:backgroundImageView atIndex:0];
[self setNeedsLayout];
}
}
- (void)setSelected:(BOOL)select {
[super setSelected:select];
// we subclass the setSelect method to highlight the background imageview
[backgroundImageView setHighlighted:select||self.highlighted];
}
- (void)setHighlighted:(BOOL)highl {
[super setHighlighted:highl];
// we subclass the setHighlighted method to highlight the background imageview
[backgroundImageView setHighlighted:highl||self.selected];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.backgroundImageView.frame = self.bounds;
}
- (void)dealloc {
[backgroundImageView release];
backgroundImageView = nil;
[super dealloc];
}
@end
版权声明:本文为博主原创文章,未经博主允许不得转载。
UIImageView xib里面拉伸图片技巧的更多相关文章
- UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图
/* 设置圆角,通过layer中的cornerRadius和masksToBounds即可. 自适应图片宽高比例.通过UIViewContentModeScaleAsp ...
- android 拉伸图片
Android拉伸图片用的是9.png格式的图片,这种图片可以指定图片的那一部分拉伸,那一部分显示内容,美工给的小图片也能有很好的显示效果. 原背景图片 可以看到原背景图片很小,即使在再长的文字,背景 ...
- ios 拉伸图片和计算文字的大小
一.拉伸图片 /** * 传入图片的名称,返回一张可拉伸不变形的图片 * * @param imageName 图片名称 * * @return 可拉伸图片 */ + (UIImage *)resiz ...
- iOS中拉伸图片的几种方式
假如下面的一张图片,是用来做按钮的背景图片的,原始尺寸是(128 * 112) 按钮背景图片.png 我们通过代码将这张图片设置为按钮的背景图片,假如我们将创建好的按钮的宽高设置为:(W=200, H ...
- iOS-使用Xcode拉伸图片
如果要制作一个类似于QQ消息气泡的图片,该如何制作呢?android中可以使用.9图片指定图片中的某一部分拉伸,那iOS中类似的功能要如何实现呢,Xcode提供了类似的功能.具体步骤如下: 1.选择需 ...
- 新浪微博登录界面上下拉伸图片--第三方开源--PullToZoomListViewEx(二)
这是PullZoomView在ScrollView实现,Android PullZoomView在ScrollView的实现是:PullToZoomScrollViewEx 下载地址:https:// ...
- QImage与QPixmap加载图片效果(QImage不能拉伸图片,QPixmap默认拉伸图片)
QImage与QPixmap加载图片 效果 . 分类: QT开发 qtQtQT PixmapTest::PixmapTest(QWidget *parent) : QDialog(parent) {t ...
- IOS学习之路二十四(UIImageView 加载gif图片)
UIImageView 怎样加载一个gif图片我还不知道(会的大神请指教),不过可以通过加载不同的图片实现gif效果 代码如下: UIImageView* animatedImageView = [[ ...
- IOS 拉伸图片(封装)
/** * 根据图片名返回一张能够自由拉伸的图片 */ +(UIImage *)resizedImage:(NSString *)name { UIImage *image=[UIImage imag ...
随机推荐
- JS中的$符号
1. 首先可以用来表示变量, 比如变量 var s='asdsd'或var $s='asdasd'; 2. 在正则表达式中,它可以匹配结尾 /sa$/.test(string) 匹配string字符串 ...
- 洛谷 P1004 方格取数 【多线程DP/四维DP/】
题目描述(https://www.luogu.org/problemnew/show/1004) 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0. ...
- Codefroces 213E. Two Permutations
E. Two Permutations time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 微信小程序之组件的集合(一)
小程序中是很强调组件中开发的,我们看到的页面是由许多组件组成的,但是这些组件是如何组合在一起的呢?来学习一下! 一.组件中数据的获取 接着上一篇文章,继续来优化代码,如何把从服务器上获取的数据显示到 ...
- HBuilderX生成本地打包App资源
http://ask.dcloud.net.cn/question/60254 概要 在HBuilderX中开发的应用可以提交到云端打包生成apk(Android平台)和ipa(iOS平台).如果本地 ...
- Session学习小结
前言: 1.我们在学习一项技术的时候,首先应该是什么时候会用到这项技术.这次学习session也是如此,一般的web项目中都有用户的存在,有用户那么就有了用户的管理,而用户往往是存在于session中 ...
- 【等价的穿越】Burnside引理&Pólya计数法
Problem 起源: SGU 294 He's Circle 遗憾的是,被吃了. Poj有道类似的: Mission 一个长度为n(1≤n≤24)的环由0,1,2组成,求有多少本质不同的环. 实际上 ...
- SDUT-3398_数据结构实验之排序一:一趟快排
数据结构实验之排序一:一趟快排 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定N个长整型范围内的整数,要求输出以给 ...
- Leetcode697.Degree of an Array数组的度
给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值. 你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度. 示例 1: 输入: [ ...
- jmeter使用Badboy录制、检查点和参数化
jmeter使用Badboy录制 1.打开Badboy,在地址栏输入需要录制的网址,如:百度首页