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 ...
随机推荐
- fastjson map转json
Map map = new HashMap(); map.put("name", "老三"); map.put("age", 12); St ...
- HDU3887 Counting Offspring [2017年6月计划 树上问题03]
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 复习解析嵌套json
在网络上找的一些经典的例子复习使用 一,解析嵌套json字符串,一个json字符串中嵌套另一个json字符串 { "msg": { "resCode": &qu ...
- cronexpr任务调度
package main import ( "github.com/gorhill/cronexpr" "fmt" "time" ) fun ...
- Python结合OpenCV学习资料
1.sunny2038的专栏 http://blog.csdn.net/sunny2038 作者建立了一个学习系列.讲得非常具体,有非常多的主要的图像处理实例. 2. https://github. ...
- sqlserver 取月初月末的时间
1.取月初的时间 --getdate() :2012/05/08 19:29:00 select convert(varchar,dateadd(day,-day(getdate())+1,ge ...
- 【模板】树的重心 洛谷P1364 医院设置
P1364 医院设置 题目描述 设有一棵二叉树,如图: 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接 ...
- window10下载安装jieba
下载安装jieba的过程如下: 1 下载jieba 2 将其解压到E:\python2,如图所示: 3 在桌面左下角搜索框中输入"运行",之后输入"cmd".再 ...
- 大合集 | 9 场 Dubbo Meetup 回顾视频和PPT下载
2018年2月16日,Dubbo 加入 Apache 基金会孵化器,在随后的那个夏天,我们开始在全国(北京.上海.深圳.成都.杭州)和社区的用户们见面.认识.交个朋友,将社区的最新动态.Dubbo 的 ...
- Codeforces 13C
这题纠结了近半年,一直没有好的思路. 刚开始看这题的时候就是暴力,明显的TLE 后来才知道这题的“一种解”肯定是"原数列中某些数的集合" (很明显这题的最优策略并不唯一) 有原数列 ...