UIImageView显示图片和图层显示

0、autoresizing使用前的解释:

UIViewAutoresizing是一个枚举类型,默认是UIViewAutoresizingNone,也就是不做任何处理。

 _bgView.contentMode =  UIViewContentModeScaleToFill;

复制代码
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
复制代码

 

各属性解释:

UIViewAutoresizingNone

不会随父视图的改变而改变

UIViewAutoresizingFlexibleLeftMargin

自动调整view与父视图左边距,以保证右边距不变

UIViewAutoresizingFlexibleWidth

自动调整view的宽度,保证左边距和右边距不变

UIViewAutoresizingFlexibleRightMargin

自动调整view与父视图右边距,以保证左边距不变

UIViewAutoresizingFlexibleTopMargin

自动调整view与父视图上边距,以保证下边距不变

UIViewAutoresizingFlexibleHeight

扫描二维码关注公众号,回复: 4787219 查看本文章
自动调整view的高度,以保证上边距和下边距不变

UIViewAutoresizingFlexibleBottomMargin

自动调整view与父视图的下边距,以保证上边距不变

 

 

 

 

 

 

 

 

 

 







UIViewContentMode 各类型效果

UIViewContentMode  

        _bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

      typedef NS_ENUM(NSInteger, UIViewContentMode) {

     但凡在设置图片模式的枚举中包含Scale这个单词的值, 都会对原有的图片进行缩放

     UIViewContentModeScaleToFill,
     按照"UIImageView"的宽高比缩放图片至图片填充整个UIImageView;

     UIViewContentModeScaleAspectFit,
     按照"图片的宽高"比例缩放图片至图片的宽度或者高度和UIImageView一样, 并且让整个图片都在UIImageView中. 然后居中显示

     UIViewContentModeScaleAspectFill,
     按照"图片的宽高"比例缩放图片至图片的宽度和高度填充整个UIImageView. 然后居中显示

     UIViewContentModeRedraw,
     UIViewContentModeCenter,
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,
     };




二 imageView图层显示

  switch ([imageViewFrames count]) {

            case 5:

                if (index == 2 ) {

                    imageView.layer.zPosition = 1;

                }else{

                    imageView.layer.zPosition = 0;

                }

                break;

            case 4:

                if (index == 0) {

                    imageView.layer.zPosition = 2;

                }else if( index == 1){

                    imageView.layer.zPosition = 1;

                }else{

                     imageView.layer.zPosition = 0;

                }

                

                break;

           

            default:

                break;

        }

 




三   图片剪裁

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

        CGImageRef maskRef = maskImage.CGImage; 

        CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
            CGImageGetHeight(maskRef),
            CGImageGetBitsPerComponent(maskRef),
            CGImageGetBitsPerPixel(maskRef),
            CGImageGetBytesPerRow(maskRef),
            CGImageGetDataProvider(maskRef), NULL, false);

        CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
        return [UIImage imageWithCGImage:masked];

    }

 
  

iOS加载网络图片


sd加载网络图片,在得到图片的回调中,进行图片的裁剪。但是需要注意得到裁剪的图片后,要注意释放,否则内存会爆掉(特别是需要处理过多裁剪的图片时)
 
  
[imageView sd_setImageWithURL:[NSURL URLWithString:[ymData.showImageArray objectAtIndex:i]] placeholderImage:[UIImage imageNamed:@"default_img.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                if (image != nil) {
                    if (image.size.height>image.size.width) {//图片的高要大于与宽
                        CGRect rect = CGRectMake(0, image.size.height/2-image.size.width/2, image.size.width, image.size.width);//创建矩形框
                        CGImageRef cgimg = CGImageCreateWithImageInRect([image CGImage], rect);
                        imageView.image=[UIImage imageWithCGImage:cgimg];
                        CGImageRelease(cgimg);
                    }else{
                        CGRect rect = CGRectMake(image.size.width/2-image.size.height/2, 0, image.size.height, image.size.height);//创建矩形框
                        CGImageRef cgimg = CGImageCreateWithImageInRect([image CGImage], rect);
                        imageView.image=[UIImage imageWithCGImage:cgimg];
                        CGImageRelease(cgimg);
                    }
                } 

}];


UIImageView显示图片


通常我们得到的图片要显示在UIImageView里。但是图片的尺寸比例跟UIImageView的比例尺寸还不一样,这时。惯性思维就是要对图片就行处理裁剪啊什么的,得到跟UIImageView比例一样的图片,这样才不会失真。其实,不用对图片做任何处理,UIImageView的属性早已搞定了一切。
 
  

imageView.contentMode = UIViewContentModeScaleAspectFill imageView.clipsToBounds = YES
UIViewContentModeScaleAspectFill会保证图片比例不变,但是是填充整个ImageView的,只有部分图片显示出来(而且需要设置clipsToBounds为YES,把多余的部分裁掉)。ImageView显示图片保证图片不失真,看起来不像压缩过的图片的方法只需设置这两个属性即可,就其他的属性自己查看SDK慢慢了解运用吧。

我们在做iOS开发的时候,往往需要实现不规则形状的头像,如:

\

那如何去实现?

通常图片都是矩形的,如果想在客户端去实现不规则的头像,需要自己去实现。

1.使用layer去实现, 见https://blog.csdn.net/johnzhjfly/article/details/39993345<喎�"/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cD4yLsq508NDQVNoYXBlTGF5ZXIsIENBTGF5ZXLI57rOyKXKtc/WPC9wPgo8cD7O0sPHwLS/tL+0yOe6zsq508NDQVNoYXBlTGF5ZXLIpcq1z9YsPC9wPgo8cD62qNLl0ru49lNoYXBlZEltYWdlVmlld6OsvMyz0NPaVUlWaWV3o6wgtPrC68jnz8KjujwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">#import "ShapedImageView.h" @interface ShapedImageView() { CALayer *_contentLayer; CAShapeLayer *_maskLayer; } @end @implementation ShapedImageView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setup]; } return self; } - (void)setup { _maskLayer = [CAShapeLayer layer]; _maskLayer.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath; _maskLayer.fillColor = [UIColor blackColor].CGColor; _maskLayer.strokeColor = [UIColor redColor].CGColor; _maskLayer.frame = self.bounds; _maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1); _maskLayer.contentsScale = [UIScreen mainScreen].scale; _contentLayer = [CALayer layer]; _contentLayer.mask = _maskLayer; _contentLayer.frame = self.bounds; [self.layer addSublayer:_contentLayer]; } - (void)setImage:(UIImage *)image { _contentLayer.contents = (id)image.CGImage; } @end 声明了用于maskLayer个CAShapedLayer, CAShapedLayer有个path的属性,将内容Layer的mask设置为maskLayer, 就可以获取到我们想要的形状。

 

path我们可以使用CAMutablePath任意的构造,上述的代码运行想过如下:

\

如果将代码改成

 

1
2
3
4
5
6
7
8
9
10
11
12
_maskLayer = [CAShapeLayer layer];
_maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius: 20 ].CGPath;
_maskLayer.fillColor = [UIColor blackColor].CGColor;
_maskLayer.strokeColor = [UIColor redColor].CGColor;
_maskLayer.frame = self.bounds;
_maskLayer.contentsCenter = CGRectMake( 0.5 , 0.5 , 0.1 , 0.1 );
_maskLayer.contentsScale = [UIScreen mainScreen].scale;                 //非常关键设置自动拉伸的效果且不变形
 
_contentLayer = [CALayer layer];
_contentLayer.mask = _maskLayer;
_contentLayer.frame = self.bounds;
[self.layer addSublayer:_contentLayer];
的效果:

 

\

如果将代码改成:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CGMutablePathRef path = CGPathCreateMutable();
CGPoint origin = self.bounds.origin;
CGFloat radius = CGRectGetWidth(self.bounds) / 2 ;
CGPathMoveToPoint(path, NULL, origin.x, origin.y + 2 *radius);
CGPathMoveToPoint(path, NULL, origin.x, origin.y + radius);
 
CGPathAddArcToPoint(path, NULL, origin.x, origin.y, origin.x + radius, origin.y, radius);
CGPathAddArcToPoint(path, NULL, origin.x + 2 * radius, origin.y, origin.x + 2 * radius, origin.y + radius, radius);
CGPathAddArcToPoint(path, NULL, origin.x + 2 * radius, origin.y + 2 * radius, origin.x + radius, origin.y + 2  * radius, radius);
CGPathAddLineToPoint(path, NULL, origin.x, origin.y + 2 * radius);
 
_maskLayer = [CAShapeLayer layer];
_maskLayer.path = path;
_maskLayer.fillColor = [UIColor blackColor].CGColor;
_maskLayer.strokeColor = [UIColor clearColor].CGColor;
_maskLayer.frame = self.bounds;
_maskLayer.contentsCenter = CGRectMake( 0.5 , 0.5 , 0.1 , 0.1 );
_maskLayer.contentsScale = [UIScreen mainScreen].scale;                 //非常关键设置自动拉伸的效果且不变形
 
_contentLayer = [CALayer layer];
_contentLayer.mask = _maskLayer;
_contentLayer.frame = self.bounds;
[self.layer addSublayer:_contentLayer];
将是这个效果:

 

\

理论上我们可以构造出任意想要的形状,但是有些形状如果你不熟悉几何知识的话是构造不出正确的

path的,从代码上我们可以看到我们可以通过设置CALayer的contents属性来设置显示的内容,那我啤"/kf/web/php/" target="_blank" class="keylink">PHPC9wPgo8cD7Kx7K7yse/ydLUzai5/cno1sNDQVNoYXBlZExheWVytcRjb250ZW50c8C0yejWw21hc2tMYXllcsTYo7+08LC4yse/z7aotcSjrLT6wuvI58/Co7o8L3A+CjxwPjwvcD4KPHByZSBjbGFzcz0="brush:java;"> _maskLayer = [CAShapeLayer layer]; _maskLayer.fillColor = [UIColor blackColor].CGColor; _maskLayer.strokeColor = [UIColor clearColor].CGColor; _maskLayer.frame = self.bounds; _maskLayer.contentsCenter = CGRectMake(0.5, 0.5, 0.1, 0.1); _maskLayer.contentsScale = [UIScreen mainScreen].scale; //非常关键设置自动拉伸的效果且不变形 _maskLayer.contents = (id)[UIImage imageNamed:@"[email protected]"].CGImage; _contentLayer = [CALayer layer]; _contentLayer.mask = _maskLayer; _contentLayer.frame = self.bounds; [self.layer addSublayer:_contentLayer];

1
 

gray_bubble_right就是你想要的形状,运行效果如下:


源代码:https://github.com/heavensword/ShapedImageView

猜你喜欢

转载自blog.csdn.net/ochenmengo/article/details/82880870