iOS 图片处理方法

作者: iOS收藏家 | 来源:发表于2016-05-31 16:56 被阅读718次

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

    相关文章

      网友评论

        本文标题:iOS 图片处理方法

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