美文网首页IOS-UI
UIImage-使用集合

UIImage-使用集合

作者: 思念那年慕云 | 来源:发表于2017-02-26 18:49 被阅读143次
    系统方法一:实现图片旋转、轴对称、缩放

    + (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;

    #cgImage:这里是传入的图片(利用方法二可以裁剪自己想要的frame(即图片的某一部分))
    #scale:1:原图;  >1:为缩小;  0-1:为放大
          注意:1,如果图片已经写死了宽高,那么这里的缩放是无效的。
               2,scale不能为负数(为负数啥也不显示了)
    #orientation:
    typedef NS_ENUM(NSInteger, UIImageOrientation) {
        UIImageOrientationUp,            // 不旋转(默认)
        UIImageOrientationDown,          // 旋转180˚(即上下颠倒)
        UIImageOrientationLeft,          // 向左旋转90˚
        UIImageOrientationRight,         // 向右旋转90˚
    
        UIImageOrientationUpMirrored,    // 不旋转(轴对称)
        UIImageOrientationDownMirrored,   // 旋转180˚(即上下颠倒,再轴对称)
        UIImageOrientationLeftMirrored,   // 向左旋转90˚,再轴对称
        UIImageOrientationRightMirrored, // 向右旋转90˚,再轴对称
    };
    
     + (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;
    
    系统方法二:截取图片的某一部分

    CGImageCreateWithImageInRect(CGImageRef image,CGRect rect)

    #image:要裁剪的图片
    #rect:裁剪的frame
    - (UIImage *)clipImageInRect:(CGRect)rect imag:(UIImage *)image
    {
        CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
        UIImage *thumbScale = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
        return thumbScale;
    }
    
    //高清,无损截图
    +(UIImage*)captureScreen:(UIView*) imageView
    {
        //第三个参数为0可以自适应屏幕,但项目中出现了崩溃,改为了1没有崩溃、
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0);
        [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return viewImage;
    }
    
    //模糊版
        /**
            截图
            1,开启图片上下文
            2,将self.view.layer渲染到当前上下文中
            3,由当前上下文输出图片
            4,关闭图片上下文
         */
        UIGraphicsBeginImageContext(self.view.bounds.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
    

    系统方法三:拉伸图片
    + (UIImage *)resizebleImageWithName:(NSString *)imageName {
        UIImage *imageNor = [UIImage imageNamed:imageName];
        CGFloat w = imageNor.size.width;
        CGFloat h = imageNor.size.height;
        imageNor = [imageNor resizableImageWithCapInsets:UIEdgeInsetsMake(w * 0.5,h * 0.5 , w * 0.5, h * 0.5) resizingMode:UIImageResizingModeStretch];
        return imageNor;
    }
    
    系统方法四:由颜色生成图片
    + (UIImage *)createImageWithColor:(UIColor *)color {
        CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
        UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return theImage;
    }
    
    方法五:图片上面添加水印的6种方式

    相关文章

      网友评论

        本文标题:UIImage-使用集合

        本文链接:https://www.haomeiwen.com/subject/jthnwttx.html