UIImageView UIImage

来源:互联网 发布:故事版软件 编辑:程序博客网 时间:2024/05/02 09:48

------------------------------------------------------------------------------------UIImage------------------------------------------------------------------------------------------

1: 将 UIImage 指定的像素以复制的方式进行拉伸.(IOS 4X 时代)

UIImage *img=[UIImage imageNamed:@"QQ20120614-2.png"];img=[img stretchableImageWithLeftCapWidth:10 topCapHeight:2];

注: 例如设定为   LeftCapWidth(左边宽度) 为 10.  那么IOS 会自动复制 位于 11 X轴 的 图像来填充图片的宽度不足的部分.

原图:


拉伸后:


2:将 UIImage 指定的像素以复制的方式进行拉伸.(IOS 5X 时代)

UIEdgeInsets edgeInsets =  UIEdgeInsetsMake(0, 10, 0, 10);img= [img resizableImageWithCapInsets:edgeInsets];

原理和4X时代的稍有不同, 这次可以借定4周的区域, 最终以中间区域来自动填充所需要阔达的区域.

注:复制的区域是图片的全比例去复制的,图片的像素大小是多少,就以多少来复制,不会默认按比例压缩.(特别注意)

那么复制的区域,如果有高清图片,会自动使用高清图片.@2x








3:如果要将 UIImage 保存的本地硬盘里  需转换为NSData 

(PNG格式)

NSData *imageData = UIImagePNGRepresentation(image);

(JPG格式)(会自动填补透明区域为白色,需要要缓存圆角图的童鞋要注意了)

data = UIImageJPEGRepresentation(image, (CGFloat)1.0);



4:将 UIImage 压缩 减少NSData 的实际大小.

+(UIImage *)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize{    // Create a graphics image context    UIGraphicsBeginImageContext(newSize);    // Tell the old image to draw in this new context, with the desired    // new size    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];    // Get the new image from the context    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();    // End the context    UIGraphicsEndImageContext();    // Return the new image.    return newImage;}



------------------------------------------------------------------------------------UIImage------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------UIImageView------------------------------------------------------------------------------------------

1:关于UIImageView 实现圆角图片的方式

两种方式:

1:直接设置 CALayer 的 圆角 和 裁剪 函数,但是用多了会影响拖动动画的流畅性

[self.image1.layer setCornerRadius:15];[self.image1.layer setMasksToBounds:YES];

2:在不影响拖动动画的流畅性下,可将UIImage通过代码以重绘的方式裁剪掉成圆角 再赋值到 UIImageView里面.

参考链接:http://no001.blog.51cto.com/1142339/637732

------------------------------------------------------------------------------------UIImageView------------------------------------------------------------------------------------------


原创粉丝点击