iOS-UIImageView和UIImage

UIImage
self.imageView.contentMode = UIViewContentModeCenter;// 图片的内容模式
[self.imageView setFrame:CGRectInset(self.bounds, 5.0, 5.0)]; //带间距的插入
 //拉伸图片

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;  四边不需要拉伸的区域

    } UIEdgeInsets;
   typedef NS_ENUM(NSInteger, UIImageResizingMode) {

    UIImageResizingModeTile,       平铺

    UIImageResizingModeStretch,    拉伸

    };

    **/
  [image resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
   [image resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeTile];
/加载图片没有缓存
[UIImage imageWithContentsOfFile:path];
//加载图片有缓存并且不会释放
[UIImage imageNamed:path];
//采取平铺方式拉伸图片,防止图片变形

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);

 
normalImage = [normalImage resizableImageWithCapInsets:insetsNormal];
一、使用文件创建(静态方法)

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 。

CGSize myImageSize = myImage.size;
 
UIImage 转 nsdata
  -(NSData *)getCoverImageDataWith:(NSString *)imgeUrl
{
    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];
}
6.13 UIImageView
UIImageView显示问题:
1. 在默认情况下,如果没有设置UIImageView的尺寸,UIImageView默认会跟图片大小一致,如果设置了尺寸,显示的图片会被压缩或者拉伸以填满整个区域
2. 如果不想要图片被拉伸或者压缩的太严重,可以给contentMode设置UIViewContentModeScaleAspectFit。
  3. 但是上面的设置,还有另外一种情况,就是图片不规则的时候,图片的宽或者高,比frame更小的时候,会出现空白,怎么解决这个问题了,可以给contentMode设置UIViewContentModeScaleAspectFill。但是这样设置还不够,因为在默认情况,图片多出来的部分还是会显示屏幕上,如果不希望超过frame的区域显示在屏幕上需要设置clipsToBounds为YES。
  4. 最后一个问题,在iPhone的retina屏幕上面,必须设置,contentScaleFactor属性。这个属性默认是1,2对应retina屏,可以通过setContentScaleFactor = [[UIScreen mainScreen] scale]来设置。
  5. UIImageView和UIView显示的区别:如果将控件添加到UIView中,如果控件的位置超出了UIView的尺寸,就不会显示。但是如果将控件添加到UIImageView,如果控件的位置超出了UIImageView的尺寸,还是会显示的。如果不想让它显示,必须设置clipsToBounds属性。
//imageWithContentsOfFile加载图片没有缓存,每次都从硬盘加载imageNamed加载会缓存,不会释放
[imageTempArray addObject:[UIImage imageWithContentsOfFile:path]];
//设置动画图片数组
self.tom.animationImages = imageTempArray;
//设置播放时长
self.tom.animationDuration = self.tom.animationImages.count*0.05;
/设置播放重复次数
self.tom.animationRepeatCount = 1;
//开始播放
[self.tom startAnimating];//播放完毕后延迟1秒释放内存
[self.tom performSelector:@selector(setAnimationImages:) 
withObject:nil afterDelay:self.tom.animationDuration + 1.0f];
原文地址:https://www.cnblogs.com/linxiu-0925/p/5058143.html