1 Android
创建ScreenShot类来进行截图处理,并返回保存的文件名
- public class ScreenShot {
- // 获取指定Activity的截屏,保存到png文件
- private static Bitmap takeScreenShot(Activity activity) {
- // View是你需要截图的View
- View view = activity.getWindow().getDecorView();
- view.setDrawingCacheEnabled(true);
- view.buildDrawingCache();
- Bitmap b1 = view.getDrawingCache();
- // 获取状态栏高度
- Rect frame = new Rect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
- int statusBarHeight = frame.top;
- Log.i("TAG", "" + statusBarHeight);
- // 获取屏幕长和高
- int width = activity.getWindowManager().getDefaultDisplay().getWidth();
- int height = activity.getWindowManager().getDefaultDisplay()
- .getHeight();
- // 去掉标题栏
- // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
- Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- - statusBarHeight);
- view.destroyDrawingCache();
- return b;
- }
- // 保存到sdcard
- private static void savePic(Bitmap b, String strFileName) {
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(strFileName);
- if (null != fos) {
- b.compress(Bitmap.CompressFormat.PNG, 90, fos);
- fos.flush();
- fos.close();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 程序入口
- public static String shoot(Activity a) {
- String strFileName = "sdcard/" + String.valueOf(System.currentTimeMillis()) + ".png";
- ScreenShot.savePic(ScreenShot.takeScreenShot(a), strFileName);
- return strFileName;
- }
- }
- public class MainActivity extends Activity {
- private Button btn;
- private String image;
- private Button btnShare;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn = (Button)findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- image = ScreenShot.shoot(MainActivity.this);
- }
- });
- btnShare = (Button) findViewById(R.id.btn_share);
- btnShare.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(Intent.ACTION_SEND);
- File file = new File(image);
- intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
- intent.setType("image/jpeg");
- Intent chooser = Intent.createChooser(intent, "Share screen shot");
- if(intent.resolveActivity(getPackageManager()) != null){
- startActivity(chooser);
- }
- }
- });
- }
- }
//分享
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(userDidTakeScreenshot:)name:UIApplicationUserDidTakeScreenshotNotificationobject:nil];
/**
* 截屏分享:截屏响应方法
*/
-(void)userDidTakeScreenshot:(NSNotification*)notification{
[TCMGoodsSharePopViewclosePopView];
[selfperformSelector:@selector(createShotScreenView)withObject:nilafterDelay:1];
}
- (void)createShotScreenView{
screenShotView = [TCMGoodsShareScreenShotViewviewFromXIB];
screenShotView.frame =CGRectMake(0,0, GDeviceScreenWidth,GDeviceScreenHeight);
UIImage *image = [UIImageimageWithData:[screenShotViewdataWithScreenshotInPNGFormat]];
//...
[[UIApplicationsharedApplication].keyWindowaddSubview:screenShotView];
//回调
[screenShotViewdisplayWithData:imagecodeUrl:theItemInfo.goodsInfo.theShareQRCodeActionBlocks:^(ShareStatusAction statusAction,UIImage *image) {
NSInteger tag = (statusAction ==ShareStatusAction_weixinTimeLine)?0:1;
[selfshareImage:image Btntag:tag];
} CancelBlock:^{
[screenShotViewremoveFromSuperview];
}];
}
#pragma mark - 分享:截屏分享和自定义图片分享
- (void)shareImage:(UIImage *)shareImage Btntag:(NSInteger)tag {
[[UMSocialManagerdefaultManager]setPlaform:UMSocialPlatformType_WechatSessionappKey:[TCMAppKeyManagertheValueForKeyType:TCM_SDK_Key_Type_AppIdforSDK:TCM_INTEGRATE_SDK_Type_WX]appSecret:[TCMAppKeyManagertheValueForKeyType:TCM_SDK_Key_Type_AppSecretforSDK:TCM_INTEGRATE_SDK_Type_WX]redirectURL:@""];
UMSocialPlatformType platformType = (tag ==0)? UMSocialPlatformType_WechatTimeLine:UMSocialPlatformType_WechatSession;
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObjectmessageObject];
UMShareImageObject* shareImgObj = [UMShareImageObjectshareObjectWithTitle:@"title"descr:@"descr"thumImage:shareImage];
shareImgObj.shareImage = shareImage;
messageObject.shareObject = shareImgObj;
//调用分享接口
[[UMSocialManagerdefaultManager] shareToPlatform:platformTypemessageObject:messageObjectcurrentViewController:selfcompletion:^(id data,NSError *error) {
//隐藏截图视图
[screenShotViewremoveFromSuperview];
[TCMGoodsSharePopViewclosePopView];
if (error) {
// [CESHelper showPromptHUBWithMsg:@"温馨提示" detailMsg:error.domain timeInterval:1.5];
// UMSocialLogInfo(@"错误回调 is %@",error.domain);
}else{
if ([dataisKindOfClass:[UMSocialShareResponseclass]]) {
UMSocialShareResponse *resp = data;
UMSocialLogInfo(@"分享结果消息response message is %@",resp.message);
UMSocialLogInfo(@"第三方原始返回的数据response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
}];
}
截屏的方法如下:
//截取当前屏幕
-(NSData *)dataWithScreenshotInPNGFormat{
CGSize imageSize =CGSizeZero;
UIInterfaceOrientation orientation = [UIApplicationsharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation))
imageSize = [UIScreenmainScreen].bounds.size;
else
imageSize = CGSizeMake([UIScreenmainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
UIGraphicsBeginImageContextWithOptions(imageSize,NO, 0);
CGContextRef context =UIGraphicsGetCurrentContext();
for (UIWindow *windowin [[UIApplicationsharedApplication] windows])
{
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
if (orientation ==UIInterfaceOrientationLandscapeLeft)
{
CGContextRotateCTM(context,M_PI_2);
CGContextTranslateCTM(context,0, -imageSize.width);
}
elseif (orientation ==UIInterfaceOrientationLandscapeRight)
{
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height,0);
} elseif (orientation ==UIInterfaceOrientationPortraitUpsideDown) {
CGContextRotateCTM(context,M_PI);
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
}
if ([windowrespondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
{
[window drawViewHierarchyInRect:window.boundsafterScreenUpdates:YES];
}
else
{
[window.layerrenderInContext:context];
}
CGContextRestoreGState(context);
}
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnUIImagePNGRepresentation(image);
}
//生成的图片
- (UIImage *)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets View:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(rect.size,NO, [UIScreenmainScreen].scale);
CGContextRef currentContext =UIGraphicsGetCurrentContext();
[view.layerrenderInContext:currentContext];
UIImage *snapshotImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *snapshotView = [[UIImageViewalloc] initWithFrame:rect];
snapshotView.image = [snapshotImageresizableImageWithCapInsets:capInsets];
// UIGraphicsBeginImageContextWithOptions(CGSizeMake(rect.size.width, rect.size.height + heightWithShuiYin), NO, [UIScreen mainScreen].scale);
// UIImageView * snapshotView = [[UIImageView alloc]initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height + heightWithShuiYin)];
return snapshotView.image;
}
//生成的图片
- (UIImage *)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets {
UIGraphicsBeginImageContextWithOptions(rect.size,NO, [UIScreenmainScreen].scale);
CGContextRef currentContext =UIGraphicsGetCurrentContext();
NSLog(@"width=%.2f,height=%.2f",self.layer.frame.size.width,self.layer.frame.size.height);
[self.layerrenderInContext:currentContext];
UIImage *snapshotImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *snapshotView = [[UIImageViewalloc] initWithFrame:rect];
snapshotView.image = [snapshotImageresizableImageWithCapInsets:capInsets];
return snapshotView.image;
}