美文网首页程序员
iOS 图片处理

iOS 图片处理

作者: A訫飛Flyme | 来源:发表于2017-11-13 17:19 被阅读55次
这就是爱一个人的眼神吧!

本文主要列出简单的图片处理代码,如:压缩图形大小,裁剪图片,添加文字水印,添加图片水印,压缩图片大小并保存。

//两张图片
slimage = [UIImage imageNamed:@"标准图.png"];
lfimage = [UIImage imageNamed:@"水印图.png"];

//压缩图形大小
[self scaleToSize:slimage size:CGSizeMake(100, 100)];
//裁剪图片
[self cutImage:slimage withRect:CGRectMake(300, 300, 100, 100)];
//添加文字水印
[self addImage:slimage text:@"SamLi" withFont:[UIFont systemFontOfSize:40.0] withRect:CGRectMake(20,20, 200, 200)];
//添加图片水印
[self addToImage:slimage image:lfimage withRect:CGRectMake(20,20, 200, 200)];

//压缩图片并保存
[self zipImageData:slimage];
//具体功能实现
#pragma mark -
//压缩图形
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size
{
    // 设置成为当前context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    return scaledImage;
}

//截图图片
- (UIImage *)cutImage:(UIImage *)image withRect:(CGRect )rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage * img = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return img;
}

//压缩图片保存
- (void)zipImageData:(UIImage *)image
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMddHHSSS"];
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    NSString *dateStr = [NSString stringWithFormat:@"%@.jpg",currentDateStr];
    
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:dateStr];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
    }
    NSData *imgData = UIImageJPEGRepresentation(image, 1);
    
    if([imgData writeToFile:path atomically:YES])
    {
        NSLog(@"saveSuccess");
    }
}
//加文字水印
- (UIImage *) addImage:(UIImage *)img text:(NSString *)mark withFont:(UIFont *)font withRect:(CGRect)rect
{
    int w = img.size.width;
    int h = img.size.height;
    
    UIGraphicsBeginImageContext(img.size);
    [[UIColor redColor] set];
    [img drawInRect:CGRectMake(0, 0, w, h)];
    
    if([[[UIDevice currentDevice]systemName]floatValue] >= 7.0)
    {
        //ios 7.0以上
        NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,[UIColor blueColor] ,NSForegroundColorAttributeName,nil];
        [mark drawInRect:rect withAttributes:dic];
    }
    else
    {
        //7.0及其以后都废弃了,完全可以不写这个判断
        [mark drawInRect:rect withFont:font];
    }
    
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
}

//加图片水印
- (UIImage *) addToImage:(UIImage *)img image:(UIImage *)newImage withRect:(CGRect)rect
{
    int w = img.size.width;
    int h = img.size.height;
    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0, w, h)];
    [newImage drawInRect:rect];
    
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return aimg;
}

本来想放GitHub,不过看看代码都在这了,还传个毛线😄

END

相关文章

  • iOS性能优化——图片加载和处理

    iOS性能优化——图片加载和处理 iOS性能优化——图片加载和处理

  • iOS 图片压缩方法

    iOS 图片压缩方法 更多图片处理方法见图片组件 BBWebImage iOS 图片压缩方法 两种图片压缩方法 两...

  • [iOS] 图像处理:一种高效裁剪图片圆角的算法

    [iOS] 图像处理:一种高效裁剪图片圆角的算法 [iOS] 图像处理:一种高效裁剪图片圆角的算法

  • imageView的处理

    图片的处理参见谈谈 iOS 中图片的解压缩

  • iOS 事件处理机制与图像渲染过程

    iOS 保持界面流畅的技巧 iOS 处理图片的一些小 Tip iOS 事件处理机制与图像渲染过程

  • iOS 图片处理

    iOS中背景图片是经常可以用到的,为了避免大图占用内存,或者控件大小视内容而定时,就必须采用图片拉伸。图片拉伸也是...

  • iOS图片处理

    iOS基于CoreML 图片风格转换

  • iOS 图片处理

    本文主要列出简单的图片处理代码,如:压缩图形大小,裁剪图片,添加文字水印,添加图片水印,压缩图片大小并保存。 本来...

  • iOS 图片处理

    iOS开发-简单图片背景替换(实现抠图效果) iOS8 Core Image In Swift:自动改善图像以及内...

  • iOS -- 图片处理

    一、目录 1> 图片拉伸处理 2> 图片扩展名 3> 大图减小高度,图片拉伸处理 4> 点击大图,Modal出来,...

网友评论

    本文标题:iOS 图片处理

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