拉伸图片的时候代码里和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
*backgroundImageView
 as property and placed it at index 0 as a subview within the UIButton.
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里面拉伸图片技巧的更多相关文章

  1. UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图

    /*      设置圆角,通过layer中的cornerRadius和masksToBounds即可.            自适应图片宽高比例.通过UIViewContentModeScaleAsp ...

  2. android 拉伸图片

    Android拉伸图片用的是9.png格式的图片,这种图片可以指定图片的那一部分拉伸,那一部分显示内容,美工给的小图片也能有很好的显示效果. 原背景图片 可以看到原背景图片很小,即使在再长的文字,背景 ...

  3. ios 拉伸图片和计算文字的大小

    一.拉伸图片 /** * 传入图片的名称,返回一张可拉伸不变形的图片 * * @param imageName 图片名称 * * @return 可拉伸图片 */ + (UIImage *)resiz ...

  4. iOS中拉伸图片的几种方式

    假如下面的一张图片,是用来做按钮的背景图片的,原始尺寸是(128 * 112) 按钮背景图片.png 我们通过代码将这张图片设置为按钮的背景图片,假如我们将创建好的按钮的宽高设置为:(W=200, H ...

  5. iOS-使用Xcode拉伸图片

    如果要制作一个类似于QQ消息气泡的图片,该如何制作呢?android中可以使用.9图片指定图片中的某一部分拉伸,那iOS中类似的功能要如何实现呢,Xcode提供了类似的功能.具体步骤如下: 1.选择需 ...

  6. 新浪微博登录界面上下拉伸图片--第三方开源--PullToZoomListViewEx(二)

    这是PullZoomView在ScrollView实现,Android PullZoomView在ScrollView的实现是:PullToZoomScrollViewEx 下载地址:https:// ...

  7. QImage与QPixmap加载图片效果(QImage不能拉伸图片,QPixmap默认拉伸图片)

    QImage与QPixmap加载图片 效果 . 分类: QT开发 qtQtQT PixmapTest::PixmapTest(QWidget *parent) : QDialog(parent) {t ...

  8. IOS学习之路二十四(UIImageView 加载gif图片)

    UIImageView 怎样加载一个gif图片我还不知道(会的大神请指教),不过可以通过加载不同的图片实现gif效果 代码如下: UIImageView* animatedImageView = [[ ...

  9. IOS 拉伸图片(封装)

    /** * 根据图片名返回一张能够自由拉伸的图片 */ +(UIImage *)resizedImage:(NSString *)name { UIImage *image=[UIImage imag ...

随机推荐

  1. Linux & CentOS & RHEL

    1.修改centos7的系统编码:https://blog.csdn.net/violet_echo_0908/article/details/58063555 2.windows 环境下使用ultr ...

  2. ORACLE的Copy命令和create table,insert into的比较

    在数据表间复制数据是Oracle DBA经常面对的任务之一,Oracle为这一任务提供了多种解决方案,SQL*Plus Copy 命令便是其中之一.SQL*Plus Copy 命令通过SQL*Net在 ...

  3. python基础--闭包and装饰器

    闭包函数:函数内部定义的函数:引用了外部变量但非全局变量 装饰器:有了闭包的概念再去理解装饰器就会相对容易一些.python装饰器本质上就是一个函数,它可以让其他函数在不需要做任何代码变动的前提下增加 ...

  4. mysql8下载安装及配置

    mysql8下载和安装 一.下载 官网地址:https://dev.mysql.com/downloads/mysql/8.0.html 选择“downloads”-->"mysql ...

  5. python枚举详解

    1. 枚举的定义 首先,定义枚举要导入enum模块. 枚举定义用class关键字,继承Enum类. 用于定义枚举的class和定义类的class是有区别[下一篇博文继续分享]. 示例代码: from ...

  6. Ubuntu 安装 RabbitMQ 和PHP扩展 - CSDN博客

    1.ubuntu16.04中安装RabbitMQ 1).首先必须要有Erlang环境支持 安装之前要装一些必要的库: sudo apt-get install build-essential  sud ...

  7. Vue--过滤器(私有和公有)

    一.过滤器的基本使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  8. hive行转列的高级用法later view explode

    先贴出一个示例: 参考链接

  9. iOS 中的 Deferred Deep Linking(延迟深度链接)

    http://www.cocoachina.com/ios/20160105/14871.html Deep Linking 其实 deep linking 并不是一个新名词,在 web 开发领域,区 ...

  10. VisualTreeHelper使用——使用BFS实现高效率的视觉对象搜索

    BFS,即广度优先搜索,是一种典型的图论算法.BFS算法与DFS(深度优先搜索)算法相对应,都是寻找图论中寻路的常用算法,两者的实现各有优点. 其中DFS算法常用递归实现,也就是常见的一条路找到黑再找 ...