1. 程式人生 > >ios開發中常用的擷取指定區域的檢視轉換成圖片

ios開發中常用的擷取指定區域的檢視轉換成圖片

 -(void)fullScreenshots{

UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];

    UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截圖,包括window

    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

}

objective c 截圖程式碼

-(void)save{

UIGraphicsBeginImageContext(mybackgroundview.bounds.size); //currentView 當前的view

[mybackgroundview.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);  //儲存到相簿中

}擷取 您想要的一層

UIGraphicsBeginImageContext(CGSizeMake(320, 300)); //currentView 當前的view

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);  //儲存到相簿中

擷取自定義的大小


iPhone開發應用中抓圖程式案例實現是本文要介紹的內容,主要是通過程式碼來實現抓圖程式,具體實現過程,一起來看詳細程式碼。

    //獲得螢幕影象  
    - (UIImage *)imageFromView: (UIView *) theView    
    {  
          
        UIGraphicsBeginImageContext(theView.frame.size);  
        CGContextRef context = UIGraphicsGetCurrentContext();  
        [theView.layer renderInContext:context];  
        UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();  
        UIGraphicsEndImageContext();  
          
        return theImage;  
    }  
     
    //獲得某個範圍內的螢幕影象  
    - (UIImage *)imageFromView: (UIView *) theView   atFrame:(CGRect)r  
    {  
        UIGraphicsBeginImageContext(theView.frame.size);  
        CGContextRef context = UIGraphicsGetCurrentContext();  
        CGContextSaveGState(context);  
        UIRectClip(r);  
        [theView.layer renderInContext:context];  
        UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();  
        UIGraphicsEndImageContext();  
          
        return  theImage;//[self getImageAreaFromImage:theImage atFrame:r];  
    }