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
{
    

    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;
    
}

你可能感兴趣的:(iOS 图片处理方法)