iOS 图片处理方法

1.图片合成

-(UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
    UIGraphicsBeginImageContext(image1.size);
    <!--两张照片合成-->    
    
    // Draw image1    
    [image1 drawInRect:CGRectMake(10, 100, image1.size.width, image1.size.height)];
    
    // Draw image2
    [image2 drawInRect:CGRectMake(200, 200, image2.size.width, image2.size.height)];
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resultingImage;
}

2.图片截屏

- (UIImage *)capture:(UIView *)view
{
    <!-- MJPhoto中有此方法一样的写法 iOS 7 之前的写法-->

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return img;
}
- (UIImage *)snapshot:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

3.图片的磨玻璃效果

-(void)blurImageWithImageView:(UIImageView *)imageView andImageName:(NSString *)imageName{

    imageView.image = [UIImage imageNamed:imageName];
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]    initWithEffect:blurEffect];
    
    effectView.frame = imageView.bounds;
    
    [imageView addSubview:effectView];
    
    effectView.alpha = 1.0f; //磨玻璃透明度的设置
    
}

4.图片的压缩

//2.保持原来的长宽比,生成一个缩略图

- (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize
{

    UIImage *newimage;
    
    if (nil == image) {
        newimage = nil;
    }
    
    else{
    
        CGSize oldsize = image.size;
        
        CGRect rect;
        
        if (asize.width/asize.height > oldsize.width/oldsize.height) {
        
            rect.size.width = asize.height*oldsize.width/oldsize.height;
            
            rect.size.height = asize.height;
            
            rect.origin.x = (asize.width - rect.size.width)/ConstEnumTwo;
            
            rect.origin.y = ConstEnumZero;
            
        }
        else{
        
            rect.size.width = asize.width;
            
            rect.size.height = asize.width*oldsize.height/oldsize.width;
            
            rect.origin.x = ConstEnumZero;
            
            rect.origin.y = (asize.height - rect.size.height)/ConstEnumTwo;            
        }        
        
        UIGraphicsBeginImageContext(asize);        
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        
        UIRectFill(CGRectMake(ConstEnumZero, ConstEnumZero, asize.width, asize.height));//clear background
        
        [image drawInRect:rect];
        
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
    }
    
    return newimage;
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,861评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,188评论 4 61
  • Actually, the booming sport market has made up a greatnum...
    Bruceshaoshao阅读 179评论 0 1
  • 我们好像许久断了联系 那段情绪我早已没了体会 回忆永远拿不来回味 截留的总是自惭形秽 所有的执着,到头...
    迷小希阅读 306评论 5 2
  • 因为是热播剧,又与现实联系密切,各大公号剧评层出不穷,诸多观点,对于罗子君的婚姻失败,归咎于全职后不思进取,那一句...
    叶落知秋凉阅读 287评论 0 1