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