iOS-UIImageView和UIImage
UIImage *normalImage = [UIImage imageNamed:@"xx.png"];
normalImage = [normalImage stretchableImageWithLeftCapWidth:normalImage.size.width / 2
topCapHeight:normalImage.size.height / 2];
/**
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right; 四边不需要拉伸的区域
UIImageResizingModeTile, 平铺
UIImageResizingModeStretch, 拉伸
};
UIImage *normalImage = [UIImage imageNamed:@"chat_send_nor"];
CGFloat normalW = normalImage.size.width;
CGFloat normalH = normalImage.size.height;
UIEdgeInsets insetsNormal = UIEdgeInsetsMake(normalH * 0.5, normalW * 0.5, normalH * 0.5, normalW * 0.5);
UIImage *myImage = [UIImage imageNamed:@"ppp"];
二、使用 URL 和原始数据(静态方法)
NSData *imageData = [ NSData initWithBytes:image:imagePtr length:imageSize ]; // 假设 imagePtr 是一个指向原始数据的指针
UIImage* myImage = [ [ UIImage alloc ]initWithData:imageData ];
UIImage *myImage2 =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
三、使用Core Graphics (静态方法)
UIImage* myImage3 = [UIImage imageWithCGImage:myCGImageRef];
四、使用文件(实例方法)
UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];
五、使用 URL 和原始数据(实例方法)
如果图像存储在内存中,你可以创建一个NSData 对象作为initWithData 方法的原始输入,来初始化一个UIImage对象。
如果图像是一张网络图片,可以使用NSData来进行预载,然后用它来初始化UIImage对象:
UIImage *myImage5 =[ [ UIImage alloc]initWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]] ];
六、使用Core Graphics (实例方法)
UIImage* myImage6 = [[UIImage alloc]initWithCGImage:myCGImageRef];
七、显示图像
当视图类的drawRect 方法被唤起时,它们会调用内部的回吐例程。与其他图像类不同,UIImage对象不能被当成子 ,直接附着在其他视图上,因为他不是一个视图类。反过来,一个UIView类则可以在视图的drawRect例程中,调用图像的 drawRect 方法。这可以使得图像显在UIView类的显示区域内部。
只要一个视图对象的窗口的某些部分需要绘制,就可以调用它的drawRect方法。要在窗口内 部显示一个 UIImage 的内容,可以调用该对象的 drawRect 方法:
- (void)drawRect:(CGRect)rect{
CGRect myRect;
myRect.origin.x = 0.0 ;
myRect.origin.y = 0.0;
myRect.size = myImage.size;
[myImage drawInRect:myRect];
}
注意不要在drawRect方法内分配任何新对象,因为他在每次窗口重绘时都被调用。
只有在视图初次绘制时,才会调用drawRect方法。要强制更新,可以使用视图类的 setNeedsDisplay 或者 setNeedsDisplayInRect 方法:
[myView setNeedsDisplay];
[myView setNeedsDisplayInRect:self.view];
八、绘制图案
如果图像是一个图案模板,你可以用UIImage类提供的另外一个方法 drawAsPatternInrect,在整个视图区域重复绘制该图像:
UIView* myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
[myImage drawInRect:myView.frame];
[self.view addSubview:myView];
九、方向
一个图像的方向,决定了它在屏幕上如何被旋转。因为iPhone 能被以6种不同的方式握持,所以在方向改变时,能够将图像做相应的旋转就十分必要了。UIImage 有个只读属性 imageOrientation 来标识它的方向。
UIImageOrientation myOrientation = myImage.imageOrientation ;
可以设置以下方向:
typedef enum {
UIImageOrientationUp, // default orientation 默认方向
UIImageOrientationDown, // 180 deg rotation 旋转180度
UIImageOrientationLeft, // 90 deg CCW 逆时针旋转90度
UIImageOrientationRight, // 90 deg CW 顺时针旋转90度
UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 向上水平翻转
UIImageOrientationDownMirrored, // horizontal flip 向下水平翻转
UIImageOrientationLeftMirrored, // vertical flip 逆时针旋转90度,垂直翻转
UIImageOrientationRightMirrored, // vertical flip 顺时针旋转90度,垂直翻转
} UIImageOrientation;
十、图像尺寸
你可以通过size属性读取一个图像的尺寸,得到一个CGSize 结构,其中包含 wifth 和height 。
{
NSURL *aUrl = [[NSURL alloc]initWithString:imgeUrl];
NSData *aData = [[NSData alloc]initWithContentsOfURL:aUrl];
if (aData==nil)
{
aData = [UIImagePNGRepresentation([UIImage imageNamed:@"image_default"]) retain];
}
SQRelease(aUrl)
return [aData autorelease];
}
iOS-UIImageView和UIImage的更多相关文章
- UIImage 和 iOS 图片压缩UIImage / UIImageVIew
UIImageView 制作气泡 stretchableImageWithLeftCapWidth http://blog.csdn.net/justinjing0612/article/detail ...
- iOS - UIImageView - how to handle UIImage image orientation
本文转载至 http://stackoverflow.com/questions/8915630/ios-uiimageview-how-to-handle-uiimage-image-orienta ...
- iOS - UIImageView
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImageView : UIView @available(iOS 2.0, *) public class U ...
- [iOS]UIImageView增加圆角
[iOS]UIImageView增加圆角 "如何给一个UIImageView增加圆角?有几种方法?各自区别?" 备注:本文参考自http://www.jianshu.com/p/d ...
- iOS开发之UIImage等比缩放
iOS开发之UIImage等比缩放 评论功能真不错 评论开通后,果然有很多人吐槽.谢谢大家的支持和关爱,如果有做的不到的地方,还请海涵.毕竟我一个人的力量是有限的,我会尽自己最大的努力大家准备一些干货 ...
- ios UIImageView处理图片大小问题
UIImageView视图可以显示图片 实例化UIImageView有两种方法 第一种方法: UIImageView *myImageView = [[ UIImageView alloc] init ...
- iOS UIImageView设置为圆形
UIImageView设置为圆形的方法(效率比较低下,当需要显示很多圆形view的时候,非常不推荐这种方式): imageView.layer.masksToBounds = YES; imageVi ...
- UIImageView 动画 / UIImage 方向
UIImage 方向 UIImage imageOrientation是相对当前屏幕的横竖屏来判断方向 如果本身是横屏, 照片也是横屏的话, 方向是正方向 BOOL b1 = (originalIma ...
- iOS UIImageView自适应图片大小
窗口大小获取: CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect CGRect rect = [ [UIScre ...
- ios UIImageView异步加载网络图片
方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...
随机推荐
- Java注解合并,注解继承
莆田SEO:spring中有时候一个类上面标记很多注解. 实际上Java注解可以进行继承(也就是把多个注解合并成1个) 比如说SpringMVC的注解 @RestController @Request ...
- HDU-1465-不容易系列之一(容斥)
链接: https://vjudge.net/problem/HDU-1465 题意: 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易 ...
- ckeditor编辑器从word粘贴公式
由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...
- CSPS模拟94
我好菜啊...... %%%迪神AK 虽然考试成绩不太好,但至少能想到正解了,也不会菜到打不出暴力. T1:想了半天不会,发现直接打高精可以拿到80分,就赶紧码完扔了,结果正解是利用double避免了 ...
- 【原创】go语言学习(五)函数详解1
目录 1.函数介绍 2.多返回值和可变参数 3.defer语句 4.内置函数介绍 1.函数介绍 1.1定义: 有输⼊入.有输出,⽤用来执⾏行行⼀一个指定任务的代码块. func functionnam ...
- 如何判断Linux下 网卡是虚拟还是物理网卡?
ifconfig命令可以查看Linux系统中正在使用的网卡,包括物理网卡和虚拟网卡,如果想要查看Linux系统中全部的网卡,可以查看/proc/net/dev文件,那如何区分网卡是虚拟还是物理的呢? ...
- C语言的历史
1.ALGOL语言 ALGOL ,为算法语言(ALGOrithmic Language)的缩写,是计算机发展史上首批产生的高级程式语言家族.当时还是晶体管计算机流行的时代,由于ALGOL语句和普通语言 ...
- 怎么看部分元素的js代码?
- Java核心复习——线程池ThreadPoolExecutor源码分析
一.线程池的介绍 线程池一种性能优化的重要手段.优化点在于创建线程和销毁线程会带来资源和时间上的消耗,而且线程池可以对线程进行管理,则可以减少这种损耗. 使用线程池的好处如下: 降低资源的消耗 提高响 ...
- 【零基础】神经网络优化之mini-batch
一.前言 回顾一下前面讲过的两种解决过拟合的方法: 1)L0.L1.L2:在向前传播.反向传播后面加个小尾巴 2)dropout:训练时随机“删除”一部分神经元 本篇要介绍的优化方法叫mini-bat ...