iOS基础之UIImageView

Posted 紫洁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS基础之UIImageView相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

IOS开发之Bug--关于UIImageView的使用

iOS开发之UIImageView的使用(swift)

iOS开发之UIImageView的使用(swift)

iOS开发之UIImageView的使用(swift)

iOS开发基础-序列帧动画之Tom猫

iOS开发之Quartz2D 六 绘制UIImageView