static void savePNGImage(CGImageRef imageRef, NSString *path)
- {
- NSURL *fileURL = [NSURL fileURLWithPath:path];
- CGImageDestinationRef dr = CGImageDestinationCreateWithURL(( CFURLRef)fileURL, kUTTypePNG , 1, NULL);
- CGImageDestinationAddImage(dr, imageRef, NULL);
- CGImageDestinationFinalize(dr);
- CFRelease(dr);
- }
- static void save()
- {
- CGDirectDisplayID displayID = CGMainDisplayID();
- CGImageRef imageRef = CGDisplayCreateImage(displayID);
- NSString *path = [@"~/Desktop/public.png" stringByExpandingTildeInPath];
- NSLog(@"save file: %@", path);
- savePNGImage(imageRef, path);
- CFRelease(imageRef);
- }
按钮点击 调用
- -(IBAction) btnScreenShot:(id) sender{
- printf("screen short start\n");
- save();
- }
关联事件后,触发后, 将在桌面保存 png 桌面截图
时间: 2024-11-13 09:55:30