码迷,mamicode.com
首页 > 移动开发 > 详细

iOS基础之UIImageView(二)

时间:2018-07-13 19:01:45      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:cgimage   imageview   wim   row   des   图片圆角   pat   bsp   save   

1 UIImage 任意角度旋转

#import "UIImage+ImageRotate.h"
#import <QuartzCore/QuartzCore.h>
#import <Accelerate/Accelerate.h>

@implementation UIImage (ImageRotate)

/**
 UIImage 任意角度旋转
 1 将图片渲染到上下文 context
 2 将 context 旋转一定的角度
 3 将旋转之后的 context 转化为 UIImage
 */
- (UIImage *)imageRaotateIndegree:(float)degree {
    // 1 将图片渲染到上下文 context
    size_t width = (size_t)self.size.width * self.scale;
    size_t height = (size_t)self.size.height * self.scale;
    
    // 每行图片数据字节
    size_t bytesPerRow = width * 4;
    // alpha 通道
    CGImageAlphaInfo alphaInfo = kCGImageAlphaPremultipliedFirst;
    // 配置上下文参数
    CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault | alphaInfo);
    if (!bmContext) {
        return nil;
    }
    CGContextDrawImage(bmContext, CGRectMake(0, 0, width, height), self.CGImage);
    // 2 将 context 旋转一定的角度
    UInt8 *data = (UInt8 *)CGBitmapContextGetData(bmContext);
    vImage_Buffer src = {data, height, width, bytesPerRow};
    vImage_Buffer dest = {data, height, width, bytesPerRow};
    Pixel_8888 bgColor = {0, 0, 0, 0};
    vImageRotate_ARGB8888(&src, &dest, NULL, degree, bgColor, kvImageBackgroundColorFill);
    // 3 将旋转之后的 context 转化为 UIImage
    CGImageRef rotateImageRef = CGBitmapContextCreateImage(bmContext);
    UIImage *rotateImage = [UIImage imageWithCGImage:rotateImageRef scale:self.scale orientation:UIImageOrientationUp];
    return rotateImage;
}

@end

2 图片任意位置裁剪

#import "UIImage+ImageCut.h"

/**
 图片任意位置裁剪
 */
- (UIImage *)imageCutSize:(CGRect)rect {
    // 1 将需要剪切的部分取出来
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    CGRect smallRect = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    // 2 将图片绘制到上下文中
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, rect, subImageRef);
    // 3 将当前上下文保存为UIImage
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

3 图片圆角剪切

#import "UIImage+ImageCircle.h"

@interface view: UIView

@property (nonatomic, strong) UIImage *image;

@end

@implementation view

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
    CGContextClip(context);
    CGContextFillPath(context);
    [_image drawAtPoint:CGPointMake(0, 0)];
    CGContextRestoreGState(context);
}

@end

@implementation UIImage (ImageCircle)

/**
 图片圆角剪切
 */
- (UIImage *)imageClipCircle {
    CGFloat imageSizeMin = MIN(self.size.width, self.size.height);
    CGSize imageSize = CGSizeMake(imageSizeMin, imageSizeMin);
    
    view *mView = [[view alloc] init];
    mView.image = self;
    
    UIGraphicsBeginImageContext(imageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    mView.frame = CGRectMake(0, 0, imageSizeMin, imageSizeMin);
    mView.backgroundColor = [UIColor whiteColor];
    [mView.layer renderInContext:context];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
@end

4 图片任意拉伸

#import "UIImage+ImageScale.h"

@implementation UIImage (ImageScale)

/**
 图片任意拉伸
 */
- (UIImage *)imageScaleSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

5 屏幕截屏

#import "UIView+ImageScreenShot.h"

@implementation UIView (ImageScreenShot)

/**
 屏幕截屏
 */
- (UIImage *)imageScreenShot {
    UIGraphicsBeginImageContext(self.frame.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return image;
}

@end

6 图片添加文字及logo水印效果

#import "UIImage+ImageWaterPrint.h"

@implementation UIImage (ImageWaterPrint)

/**
 图片添加文字及logo水印效果
 */
- (UIImage *)imageWater:(UIImage *)imageLogo waterString:(NSString *)waterString {
    UIGraphicsBeginImageContext(self.size);
    // 1 原始图片渲染
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    
    // 2 将logo渲染到右下角
    [imageLogo drawInRect:CGRectMake(self.size.width - 20, self.size.height - 20, 16, 16)];
    
    // 3 将文字渲染到左上角
    NSMutableParagraphStyle *paragraphStype = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStype.lineBreakMode = NSLineBreakByCharWrapping;
    NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:20],
                           NSForegroundColorAttributeName: [UIColor redColor],
                           NSParagraphStyleAttributeName: paragraphStype};
    [waterString drawInRect:CGRectMake(0, 0, 300, 50) withAttributes:dict];
    
    // 4 将当前上下文保存为UIImage
    UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return imageNew;
}

@end

 

iOS基础之UIImageView(二)

标签:cgimage   imageview   wim   row   des   图片圆角   pat   bsp   save   

原文地址:https://www.cnblogs.com/muzijie/p/9306379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!