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中有此方法一樣的寫(xiě)法 iOS 7 之前的寫(xiě)法-->

    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; //磨玻璃透明度的設(shè)置
    
}

4.圖片的壓縮

//2.保持原來(lái)的長(zhǎng)寬比,生成一個(gè)縮略圖

- (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;
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,917評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,172評(píng)論 4 61
  • Actually, the booming sport market has made up a greatnum...
    Bruceshaoshao閱讀 206評(píng)論 0 1
  • 我們好像許久斷了聯(lián)系 那段情緒我早已沒(méi)了體會(huì) 回憶永遠(yuǎn)拿不來(lái)回味 截留的總是自慚形穢 所有的執(zhí)著,到頭...
    迷小希閱讀 348評(píng)論 5 2
  • 因?yàn)槭菬岵?,又與現(xiàn)實(shí)聯(lián)系密切,各大公號(hào)劇評(píng)層出不窮,諸多觀點(diǎn),對(duì)于羅子君的婚姻失敗,歸咎于全職后不思進(jìn)取,那一句...
    葉落知秋涼閱讀 355評(píng)論 0 1

友情鏈接更多精彩內(nèi)容