├── README.md ├── ShotScreen.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── douxindong.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── douxindong.xcuserdatad │ └── xcschemes │ ├── ShotScreen.xcscheme │ └── xcschememanagement.plist └── ShotScreen ├── 1487513280.png ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m ├── default_logo@3x.png ├── img_01@3x.png └── main.m /README.md: -------------------------------------------------------------------------------- 1 | # screenShot 2 | iOS截取特定区域的图片,然后拼接起来,可在图片上定制任意控件 3 | >基于这样的截图 4 | 5 | ![WechatIMG7.png](http://upload-images.jianshu.io/upload_images/3729815-7801d7ee86395599.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/240) 6 | >分享之后 7 | 8 | ![WechatIMG6.png](http://upload-images.jianshu.io/upload_images/3729815-674eac15b7ed5aad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/240) 9 | 10 | ``` 11 | 分析:这是由三部分组成的 12 | ``` 13 | ``` 14 | 1.headerImageView(头部的控件) 15 | ``` 16 | ``` 17 | 2.midView(中间的控件) 18 | ``` 19 | ``` 20 | 3.footerImageView(下部的控件) 21 | ``` 22 | >下面来上代码 23 | 24 | ``` 25 | #import "ViewController.h" 26 | 27 | @interface ViewController () 28 | /** 头部 */ 29 | @property (nonatomic,strong) UIImageView *headerImageView; 30 | /** 中部 */ 31 | @property (nonatomic,strong) UIView *midView; 32 | /** 下部 */ 33 | @property (nonatomic,strong) UIImageView *footerImageView; 34 | 35 | @end 36 | 37 | @implementation ViewController 38 | -(UIImageView *)headerImageView{ 39 | if (_headerImageView == nil) { 40 | _headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_logo@3x.png"]]; 41 | _headerImageView.frame = CGRectMake(0, 0, self.view.frame.size.width, 200); 42 | } 43 | return _headerImageView; 44 | } 45 | -(UIView *)midView{ 46 | if (_midView == nil) { 47 | _midView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerImageView.frame), self.view.frame.size.width, 200)]; 48 | [_midView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"img_01@3x.png"]]]; 49 | 50 | } 51 | return _midView; 52 | } 53 | -(UIImageView *)footerImageView{ 54 | if (_footerImageView == nil) { 55 | _footerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1487513280.png"]]; 56 | _footerImageView.frame = CGRectMake(0, CGRectGetMaxY(self.midView.frame), self.view.frame.size.width, 200); 57 | } 58 | return _footerImageView; 59 | } 60 | 61 | ``` 62 | ``` 63 | - (void)viewDidLoad { 64 | [super viewDidLoad]; 65 | // Do any additional setup after loading the view, typically from a nib. 66 | 67 | } 68 | -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 69 | [self screenShot]; 70 | } 71 | -(void)screenShot{ 72 | 73 | 74 | UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"保存图片到相册?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles: nil]; 75 | [actionsheet showInView:self.view]; 76 | 77 | actionsheet = nil; 78 | 79 | } 80 | 81 | ``` 82 | ``` 83 | //保存图片 84 | - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { 85 | if (buttonIndex != [actionSheet cancelButtonIndex] ) { 86 | [self.view addSubview:self.headerImageView]; 87 | UIImage *headerImage = [self screenShotWithSize:self.headerImageView.frame.size Layer:self.headerImageView.layer]; 88 | [self.headerImageView removeFromSuperview]; 89 | [self.view addSubview:self.midView]; 90 | UIImage *midImage = [self screenShotWithSize:self.midView.frame.size Layer:self.midView.layer]; 91 | [self.midView removeFromSuperview]; 92 | [self.view addSubview:self.footerImageView]; 93 | UIImage *footerImage = [self screenShotWithSize:self.footerImageView.frame.size Layer:self.footerImageView.layer]; 94 | [self.footerImageView removeFromSuperview]; 95 | 96 | UIImage *saveImage = [self addSlaveHeaderImage:headerImage toMasterMidImage:midImage toMasterFootImage:footerImage]; 97 | 98 | UIImageWriteToSavedPhotosAlbum(saveImage, self, nil, nil); 99 | } 100 | return; 101 | } 102 | /* * 103 | * masterImage 主图片,生成的图片的宽度为masterImage的宽度 104 | * slaveImage 从图片,拼接在masterImage的下面 105 | */ 106 | - (UIImage *)addSlaveHeaderImage:(UIImage *)slaveheaderImage toMasterMidImage:(UIImage *)mastermidImage toMasterFootImage:(UIImage *)masterfootImage{ 107 | CGSize size; 108 | size.width = slaveheaderImage.size.width; 109 | size.height = slaveheaderImage.size.height + mastermidImage.size.height + masterfootImage.size.height; 110 | 111 | UIGraphicsBeginImageContextWithOptions(size, YES, 0.0); 112 | 113 | //Draw slaveheaderImage 114 | [slaveheaderImage drawInRect:CGRectMake(0, 0, slaveheaderImage.size.width, slaveheaderImage.size.height)]; 115 | 116 | //Draw mastermidImage 117 | [mastermidImage drawInRect:CGRectMake(0, mastermidImage.size.height, mastermidImage.size.width, mastermidImage.size.height)]; 118 | 119 | //Draw masterfootImage 120 | [masterfootImage drawInRect:CGRectMake(0, slaveheaderImage.size.height+masterfootImage.size.height, masterfootImage.size.width, masterfootImage.size.height)]; 121 | 122 | UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 123 | 124 | UIGraphicsEndImageContext(); 125 | 126 | return resultImage; 127 | } 128 | //截取定点位置屏幕图片 129 | - (UIImage *)screenShotWithSize:(CGSize)size Layer:(CALayer *)layer{ 130 | UIImage* image = nil; 131 | /* 132 | *UIGraphicsBeginImageContextWithOptions有三个参数 133 | *size bitmap上下文的大小,就是生成图片的size 134 | *opaque 是否不透明,当指定为YES的时候图片的质量会比较好 135 | *scale 缩放比例,指定为0.0表示使用手机主屏幕的缩放比例 136 | */ 137 | UIGraphicsBeginImageContextWithOptions(size, YES, 0.0); 138 | //此处我截取layer. 139 | [layer renderInContext: UIGraphicsGetCurrentContext()]; 140 | 141 | image = UIGraphicsGetImageFromCurrentImageContext(); 142 | UIGraphicsEndImageContext(); 143 | 144 | if (image != nil) { 145 | return image; 146 | }else { 147 | return nil; 148 | } 149 | } 150 | - (void)didReceiveMemoryWarning { 151 | [super didReceiveMemoryWarning]; 152 | // Dispose of any resources that can be recreated. 153 | } 154 | ``` 155 | >效果图 156 | ![Simulator Screen Shot 2017年3月10日 下午12.19.31.png](http://upload-images.jianshu.io/upload_images/3729815-90f85c13f45ebed9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/240) 157 | -------------------------------------------------------------------------------- /ShotScreen.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | FE6EF3741E7251880029951B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6EF3731E7251880029951B /* main.m */; }; 11 | FE6EF3771E7251880029951B /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6EF3761E7251880029951B /* AppDelegate.m */; }; 12 | FE6EF37A1E7251880029951B /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6EF3791E7251880029951B /* ViewController.m */; }; 13 | FE6EF37D1E7251880029951B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FE6EF37B1E7251880029951B /* Main.storyboard */; }; 14 | FE6EF37F1E7251880029951B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FE6EF37E1E7251880029951B /* Assets.xcassets */; }; 15 | FE6EF3821E7251880029951B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FE6EF3801E7251880029951B /* LaunchScreen.storyboard */; }; 16 | FE6EF38A1E7252A20029951B /* default_logo@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = FE6EF3891E7252A20029951B /* default_logo@3x.png */; }; 17 | FE6EF38C1E7252B60029951B /* img_01@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = FE6EF38B1E7252B60029951B /* img_01@3x.png */; }; 18 | FE6EF38E1E7252BD0029951B /* 1487513280.png in Resources */ = {isa = PBXBuildFile; fileRef = FE6EF38D1E7252BD0029951B /* 1487513280.png */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXFileReference section */ 22 | FE6EF36F1E7251880029951B /* ShotScreen.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ShotScreen.app; sourceTree = BUILT_PRODUCTS_DIR; }; 23 | FE6EF3731E7251880029951B /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 24 | FE6EF3751E7251880029951B /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 25 | FE6EF3761E7251880029951B /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 26 | FE6EF3781E7251880029951B /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 27 | FE6EF3791E7251880029951B /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 28 | FE6EF37C1E7251880029951B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 29 | FE6EF37E1E7251880029951B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 30 | FE6EF3811E7251880029951B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 31 | FE6EF3831E7251880029951B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32 | FE6EF3891E7252A20029951B /* default_logo@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "default_logo@3x.png"; sourceTree = ""; }; 33 | FE6EF38B1E7252B60029951B /* img_01@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "img_01@3x.png"; sourceTree = ""; }; 34 | FE6EF38D1E7252BD0029951B /* 1487513280.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 1487513280.png; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | FE6EF36C1E7251880029951B /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | FE6EF3661E7251880029951B = { 49 | isa = PBXGroup; 50 | children = ( 51 | FE6EF3711E7251880029951B /* ShotScreen */, 52 | FE6EF3701E7251880029951B /* Products */, 53 | ); 54 | sourceTree = ""; 55 | }; 56 | FE6EF3701E7251880029951B /* Products */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | FE6EF36F1E7251880029951B /* ShotScreen.app */, 60 | ); 61 | name = Products; 62 | sourceTree = ""; 63 | }; 64 | FE6EF3711E7251880029951B /* ShotScreen */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | FE6EF3891E7252A20029951B /* default_logo@3x.png */, 68 | FE6EF38B1E7252B60029951B /* img_01@3x.png */, 69 | FE6EF38D1E7252BD0029951B /* 1487513280.png */, 70 | FE6EF3751E7251880029951B /* AppDelegate.h */, 71 | FE6EF3761E7251880029951B /* AppDelegate.m */, 72 | FE6EF3781E7251880029951B /* ViewController.h */, 73 | FE6EF3791E7251880029951B /* ViewController.m */, 74 | FE6EF37B1E7251880029951B /* Main.storyboard */, 75 | FE6EF37E1E7251880029951B /* Assets.xcassets */, 76 | FE6EF3801E7251880029951B /* LaunchScreen.storyboard */, 77 | FE6EF3831E7251880029951B /* Info.plist */, 78 | FE6EF3721E7251880029951B /* Supporting Files */, 79 | ); 80 | path = ShotScreen; 81 | sourceTree = ""; 82 | }; 83 | FE6EF3721E7251880029951B /* Supporting Files */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | FE6EF3731E7251880029951B /* main.m */, 87 | ); 88 | name = "Supporting Files"; 89 | sourceTree = ""; 90 | }; 91 | /* End PBXGroup section */ 92 | 93 | /* Begin PBXNativeTarget section */ 94 | FE6EF36E1E7251880029951B /* ShotScreen */ = { 95 | isa = PBXNativeTarget; 96 | buildConfigurationList = FE6EF3861E7251880029951B /* Build configuration list for PBXNativeTarget "ShotScreen" */; 97 | buildPhases = ( 98 | FE6EF36B1E7251880029951B /* Sources */, 99 | FE6EF36C1E7251880029951B /* Frameworks */, 100 | FE6EF36D1E7251880029951B /* Resources */, 101 | ); 102 | buildRules = ( 103 | ); 104 | dependencies = ( 105 | ); 106 | name = ShotScreen; 107 | productName = ShotScreen; 108 | productReference = FE6EF36F1E7251880029951B /* ShotScreen.app */; 109 | productType = "com.apple.product-type.application"; 110 | }; 111 | /* End PBXNativeTarget section */ 112 | 113 | /* Begin PBXProject section */ 114 | FE6EF3671E7251880029951B /* Project object */ = { 115 | isa = PBXProject; 116 | attributes = { 117 | LastUpgradeCheck = 0820; 118 | ORGANIZATIONNAME = "窦心东"; 119 | TargetAttributes = { 120 | FE6EF36E1E7251880029951B = { 121 | CreatedOnToolsVersion = 8.2.1; 122 | DevelopmentTeam = L7TDQ3CJL8; 123 | ProvisioningStyle = Automatic; 124 | }; 125 | }; 126 | }; 127 | buildConfigurationList = FE6EF36A1E7251880029951B /* Build configuration list for PBXProject "ShotScreen" */; 128 | compatibilityVersion = "Xcode 3.2"; 129 | developmentRegion = English; 130 | hasScannedForEncodings = 0; 131 | knownRegions = ( 132 | en, 133 | Base, 134 | ); 135 | mainGroup = FE6EF3661E7251880029951B; 136 | productRefGroup = FE6EF3701E7251880029951B /* Products */; 137 | projectDirPath = ""; 138 | projectRoot = ""; 139 | targets = ( 140 | FE6EF36E1E7251880029951B /* ShotScreen */, 141 | ); 142 | }; 143 | /* End PBXProject section */ 144 | 145 | /* Begin PBXResourcesBuildPhase section */ 146 | FE6EF36D1E7251880029951B /* Resources */ = { 147 | isa = PBXResourcesBuildPhase; 148 | buildActionMask = 2147483647; 149 | files = ( 150 | FE6EF38E1E7252BD0029951B /* 1487513280.png in Resources */, 151 | FE6EF3821E7251880029951B /* LaunchScreen.storyboard in Resources */, 152 | FE6EF38C1E7252B60029951B /* img_01@3x.png in Resources */, 153 | FE6EF37F1E7251880029951B /* Assets.xcassets in Resources */, 154 | FE6EF38A1E7252A20029951B /* default_logo@3x.png in Resources */, 155 | FE6EF37D1E7251880029951B /* Main.storyboard in Resources */, 156 | ); 157 | runOnlyForDeploymentPostprocessing = 0; 158 | }; 159 | /* End PBXResourcesBuildPhase section */ 160 | 161 | /* Begin PBXSourcesBuildPhase section */ 162 | FE6EF36B1E7251880029951B /* Sources */ = { 163 | isa = PBXSourcesBuildPhase; 164 | buildActionMask = 2147483647; 165 | files = ( 166 | FE6EF37A1E7251880029951B /* ViewController.m in Sources */, 167 | FE6EF3771E7251880029951B /* AppDelegate.m in Sources */, 168 | FE6EF3741E7251880029951B /* main.m in Sources */, 169 | ); 170 | runOnlyForDeploymentPostprocessing = 0; 171 | }; 172 | /* End PBXSourcesBuildPhase section */ 173 | 174 | /* Begin PBXVariantGroup section */ 175 | FE6EF37B1E7251880029951B /* Main.storyboard */ = { 176 | isa = PBXVariantGroup; 177 | children = ( 178 | FE6EF37C1E7251880029951B /* Base */, 179 | ); 180 | name = Main.storyboard; 181 | sourceTree = ""; 182 | }; 183 | FE6EF3801E7251880029951B /* LaunchScreen.storyboard */ = { 184 | isa = PBXVariantGroup; 185 | children = ( 186 | FE6EF3811E7251880029951B /* Base */, 187 | ); 188 | name = LaunchScreen.storyboard; 189 | sourceTree = ""; 190 | }; 191 | /* End PBXVariantGroup section */ 192 | 193 | /* Begin XCBuildConfiguration section */ 194 | FE6EF3841E7251880029951B /* Debug */ = { 195 | isa = XCBuildConfiguration; 196 | buildSettings = { 197 | ALWAYS_SEARCH_USER_PATHS = NO; 198 | CLANG_ANALYZER_NONNULL = YES; 199 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 200 | CLANG_CXX_LIBRARY = "libc++"; 201 | CLANG_ENABLE_MODULES = YES; 202 | CLANG_ENABLE_OBJC_ARC = YES; 203 | CLANG_WARN_BOOL_CONVERSION = YES; 204 | CLANG_WARN_CONSTANT_CONVERSION = YES; 205 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 206 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 207 | CLANG_WARN_EMPTY_BODY = YES; 208 | CLANG_WARN_ENUM_CONVERSION = YES; 209 | CLANG_WARN_INFINITE_RECURSION = YES; 210 | CLANG_WARN_INT_CONVERSION = YES; 211 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 212 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 213 | CLANG_WARN_UNREACHABLE_CODE = YES; 214 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 215 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 216 | COPY_PHASE_STRIP = NO; 217 | DEBUG_INFORMATION_FORMAT = dwarf; 218 | ENABLE_STRICT_OBJC_MSGSEND = YES; 219 | ENABLE_TESTABILITY = YES; 220 | GCC_C_LANGUAGE_STANDARD = gnu99; 221 | GCC_DYNAMIC_NO_PIC = NO; 222 | GCC_NO_COMMON_BLOCKS = YES; 223 | GCC_OPTIMIZATION_LEVEL = 0; 224 | GCC_PREPROCESSOR_DEFINITIONS = ( 225 | "DEBUG=1", 226 | "$(inherited)", 227 | ); 228 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 229 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 230 | GCC_WARN_UNDECLARED_SELECTOR = YES; 231 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 232 | GCC_WARN_UNUSED_FUNCTION = YES; 233 | GCC_WARN_UNUSED_VARIABLE = YES; 234 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 235 | MTL_ENABLE_DEBUG_INFO = YES; 236 | ONLY_ACTIVE_ARCH = YES; 237 | SDKROOT = iphoneos; 238 | TARGETED_DEVICE_FAMILY = "1,2"; 239 | }; 240 | name = Debug; 241 | }; 242 | FE6EF3851E7251880029951B /* Release */ = { 243 | isa = XCBuildConfiguration; 244 | buildSettings = { 245 | ALWAYS_SEARCH_USER_PATHS = NO; 246 | CLANG_ANALYZER_NONNULL = YES; 247 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 248 | CLANG_CXX_LIBRARY = "libc++"; 249 | CLANG_ENABLE_MODULES = YES; 250 | CLANG_ENABLE_OBJC_ARC = YES; 251 | CLANG_WARN_BOOL_CONVERSION = YES; 252 | CLANG_WARN_CONSTANT_CONVERSION = YES; 253 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 254 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 255 | CLANG_WARN_EMPTY_BODY = YES; 256 | CLANG_WARN_ENUM_CONVERSION = YES; 257 | CLANG_WARN_INFINITE_RECURSION = YES; 258 | CLANG_WARN_INT_CONVERSION = YES; 259 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 260 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 261 | CLANG_WARN_UNREACHABLE_CODE = YES; 262 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 263 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 264 | COPY_PHASE_STRIP = NO; 265 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 266 | ENABLE_NS_ASSERTIONS = NO; 267 | ENABLE_STRICT_OBJC_MSGSEND = YES; 268 | GCC_C_LANGUAGE_STANDARD = gnu99; 269 | GCC_NO_COMMON_BLOCKS = YES; 270 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 271 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 272 | GCC_WARN_UNDECLARED_SELECTOR = YES; 273 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 274 | GCC_WARN_UNUSED_FUNCTION = YES; 275 | GCC_WARN_UNUSED_VARIABLE = YES; 276 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 277 | MTL_ENABLE_DEBUG_INFO = NO; 278 | SDKROOT = iphoneos; 279 | TARGETED_DEVICE_FAMILY = "1,2"; 280 | VALIDATE_PRODUCT = YES; 281 | }; 282 | name = Release; 283 | }; 284 | FE6EF3871E7251880029951B /* Debug */ = { 285 | isa = XCBuildConfiguration; 286 | buildSettings = { 287 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 288 | DEVELOPMENT_TEAM = L7TDQ3CJL8; 289 | INFOPLIST_FILE = ShotScreen/Info.plist; 290 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 291 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 292 | PRODUCT_BUNDLE_IDENTIFIER = com.dxd.ShotScreen; 293 | PRODUCT_NAME = "$(TARGET_NAME)"; 294 | }; 295 | name = Debug; 296 | }; 297 | FE6EF3881E7251880029951B /* Release */ = { 298 | isa = XCBuildConfiguration; 299 | buildSettings = { 300 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 301 | DEVELOPMENT_TEAM = L7TDQ3CJL8; 302 | INFOPLIST_FILE = ShotScreen/Info.plist; 303 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 304 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 305 | PRODUCT_BUNDLE_IDENTIFIER = com.dxd.ShotScreen; 306 | PRODUCT_NAME = "$(TARGET_NAME)"; 307 | }; 308 | name = Release; 309 | }; 310 | /* End XCBuildConfiguration section */ 311 | 312 | /* Begin XCConfigurationList section */ 313 | FE6EF36A1E7251880029951B /* Build configuration list for PBXProject "ShotScreen" */ = { 314 | isa = XCConfigurationList; 315 | buildConfigurations = ( 316 | FE6EF3841E7251880029951B /* Debug */, 317 | FE6EF3851E7251880029951B /* Release */, 318 | ); 319 | defaultConfigurationIsVisible = 0; 320 | defaultConfigurationName = Release; 321 | }; 322 | FE6EF3861E7251880029951B /* Build configuration list for PBXNativeTarget "ShotScreen" */ = { 323 | isa = XCConfigurationList; 324 | buildConfigurations = ( 325 | FE6EF3871E7251880029951B /* Debug */, 326 | FE6EF3881E7251880029951B /* Release */, 327 | ); 328 | defaultConfigurationIsVisible = 0; 329 | }; 330 | /* End XCConfigurationList section */ 331 | }; 332 | rootObject = FE6EF3671E7251880029951B /* Project object */; 333 | } 334 | -------------------------------------------------------------------------------- /ShotScreen.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ShotScreen.xcodeproj/project.xcworkspace/xcuserdata/douxindong.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/douxindong/screenShot/7bdc0e5415539ac4730fd362e52e67a08b6ae6bd/ShotScreen.xcodeproj/project.xcworkspace/xcuserdata/douxindong.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /ShotScreen.xcodeproj/xcuserdata/douxindong.xcuserdatad/xcschemes/ShotScreen.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /ShotScreen.xcodeproj/xcuserdata/douxindong.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | ShotScreen.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | FE6EF36E1E7251880029951B 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /ShotScreen/1487513280.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/douxindong/screenShot/7bdc0e5415539ac4730fd362e52e67a08b6ae6bd/ShotScreen/1487513280.png -------------------------------------------------------------------------------- /ShotScreen/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // ShotScreen 4 | // 5 | // Created by 窦心东 on 2017/3/10. 6 | // Copyright © 2017年 窦心东. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /ShotScreen/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // ShotScreen 4 | // 5 | // Created by 窦心东 on 2017/3/10. 6 | // Copyright © 2017年 窦心东. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /ShotScreen/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /ShotScreen/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /ShotScreen/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /ShotScreen/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | NSAppleMusicUsageDescription 45 | App需要您的同意,才能访问媒体资料库 46 | NSBluetoothPeripheralUsageDescription 47 | App需要您的同意,才能访问蓝牙 48 | NSCalendarsUsageDescription 49 | App需要您的同意,才能访问日历 50 | NSCameraUsageDescription 51 | App需要您的同意,才能访问相机 52 | NSContactsUsageDescription 53 | 是否允许此App访问你的通讯录 54 | NSHealthShareUsageDescription 55 | App需要您的同意,才能访问健康分享 56 | NSHealthUpdateUsageDescription 57 | App需要您的同意,才能访问健康更新 58 | NSLocationAlwaysUsageDescription 59 | App需要您的同意,才能始终访问位置 60 | NSLocationUsageDescription 61 | App需要您的同意,才能访问位置 62 | NSLocationWhenInUseUsageDescription 63 | App需要您的同意,才能在使用期间访问位置 64 | NSMicrophoneUsageDescription 65 | App需要您的同意,才能访问麦克风 66 | NSMotionUsageDescription 67 | App需要您的同意,才能访问运动与健身 68 | NSPhotoLibraryUsageDescription 69 | App需要您的同意,才能访问相册 70 | NSRemindersUsageDescription 71 | App需要您的同意,才能访问提醒事项 72 | 73 | 74 | -------------------------------------------------------------------------------- /ShotScreen/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // ShotScreen 4 | // 5 | // Created by 窦心东 on 2017/3/10. 6 | // Copyright © 2017年 窦心东. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /ShotScreen/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // ShotScreen 4 | // 5 | // Created by 窦心东 on 2017/3/10. 6 | // Copyright © 2017年 窦心东. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | /** 头部 */ 13 | @property (nonatomic,strong) UIImageView *headerImageView; 14 | /** 中部 */ 15 | @property (nonatomic,strong) UIView *midView; 16 | /** 下部 */ 17 | @property (nonatomic,strong) UIImageView *footerImageView; 18 | 19 | @end 20 | 21 | @implementation ViewController 22 | -(UIImageView *)headerImageView{ 23 | if (_headerImageView == nil) { 24 | _headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_logo@3x.png"]]; 25 | _headerImageView.frame = CGRectMake(0, 0, self.view.frame.size.width, 200); 26 | } 27 | return _headerImageView; 28 | } 29 | -(UIView *)midView{ 30 | if (_midView == nil) { 31 | _midView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerImageView.frame), self.view.frame.size.width, 200)]; 32 | [_midView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"img_01@3x.png"]]]; 33 | 34 | } 35 | return _midView; 36 | } 37 | -(UIImageView *)footerImageView{ 38 | if (_footerImageView == nil) { 39 | _footerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1487513280.png"]]; 40 | _footerImageView.frame = CGRectMake(0, CGRectGetMaxY(self.midView.frame), self.view.frame.size.width, 200); 41 | } 42 | return _footerImageView; 43 | } 44 | - (void)viewDidLoad { 45 | [super viewDidLoad]; 46 | // Do any additional setup after loading the view, typically from a nib. 47 | 48 | } 49 | -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 50 | [self screenShot]; 51 | } 52 | -(void)screenShot{ 53 | 54 | 55 | UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"保存图片到相册?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles: nil]; 56 | [actionsheet showInView:self.view]; 57 | 58 | actionsheet = nil; 59 | 60 | } 61 | //保存图片 62 | - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { 63 | if (buttonIndex != [actionSheet cancelButtonIndex] ) { 64 | [self.view addSubview:self.headerImageView]; 65 | UIImage *headerImage = [self screenShotWithSize:self.headerImageView.frame.size Layer:self.headerImageView.layer]; 66 | [self.headerImageView removeFromSuperview]; 67 | [self.view addSubview:self.midView]; 68 | UIImage *midImage = [self screenShotWithSize:self.midView.frame.size Layer:self.midView.layer]; 69 | [self.midView removeFromSuperview]; 70 | [self.view addSubview:self.footerImageView]; 71 | UIImage *footerImage = [self screenShotWithSize:self.footerImageView.frame.size Layer:self.footerImageView.layer]; 72 | [self.footerImageView removeFromSuperview]; 73 | 74 | UIImage *saveImage = [self addSlaveHeaderImage:headerImage toMasterMidImage:midImage toMasterFootImage:footerImage]; 75 | 76 | UIImageWriteToSavedPhotosAlbum(saveImage, self, nil, nil); 77 | } 78 | return; 79 | } 80 | /* * 81 | * masterImage 主图片,生成的图片的宽度为masterImage的宽度 82 | * slaveImage 从图片,拼接在masterImage的下面 83 | */ 84 | - (UIImage *)addSlaveHeaderImage:(UIImage *)slaveheaderImage toMasterMidImage:(UIImage *)mastermidImage toMasterFootImage:(UIImage *)masterfootImage{ 85 | CGSize size; 86 | size.width = slaveheaderImage.size.width; 87 | size.height = slaveheaderImage.size.height + mastermidImage.size.height + masterfootImage.size.height; 88 | 89 | UIGraphicsBeginImageContextWithOptions(size, YES, 0.0); 90 | 91 | //Draw slaveheaderImage 92 | [slaveheaderImage drawInRect:CGRectMake(0, 0, slaveheaderImage.size.width, slaveheaderImage.size.height)]; 93 | 94 | //Draw mastermidImage 95 | [mastermidImage drawInRect:CGRectMake(0, mastermidImage.size.height, mastermidImage.size.width, mastermidImage.size.height)]; 96 | 97 | //Draw masterfootImage 98 | [masterfootImage drawInRect:CGRectMake(0, slaveheaderImage.size.height+masterfootImage.size.height, masterfootImage.size.width, masterfootImage.size.height)]; 99 | 100 | UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 101 | 102 | UIGraphicsEndImageContext(); 103 | 104 | return resultImage; 105 | } 106 | //截取定点位置屏幕图片 107 | - (UIImage *)screenShotWithSize:(CGSize)size Layer:(CALayer *)layer{ 108 | UIImage* image = nil; 109 | /* 110 | *UIGraphicsBeginImageContextWithOptions有三个参数 111 | *size bitmap上下文的大小,就是生成图片的size 112 | *opaque 是否不透明,当指定为YES的时候图片的质量会比较好 113 | *scale 缩放比例,指定为0.0表示使用手机主屏幕的缩放比例 114 | */ 115 | UIGraphicsBeginImageContextWithOptions(size, YES, 0.0); 116 | //此处我截取layer. 117 | [layer renderInContext: UIGraphicsGetCurrentContext()]; 118 | 119 | image = UIGraphicsGetImageFromCurrentImageContext(); 120 | UIGraphicsEndImageContext(); 121 | 122 | if (image != nil) { 123 | return image; 124 | }else { 125 | return nil; 126 | } 127 | } 128 | - (void)didReceiveMemoryWarning { 129 | [super didReceiveMemoryWarning]; 130 | // Dispose of any resources that can be recreated. 131 | } 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | @end 156 | -------------------------------------------------------------------------------- /ShotScreen/default_logo@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/douxindong/screenShot/7bdc0e5415539ac4730fd362e52e67a08b6ae6bd/ShotScreen/default_logo@3x.png -------------------------------------------------------------------------------- /ShotScreen/img_01@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/douxindong/screenShot/7bdc0e5415539ac4730fd362e52e67a08b6ae6bd/ShotScreen/img_01@3x.png -------------------------------------------------------------------------------- /ShotScreen/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // ShotScreen 4 | // 5 | // Created by 窦心东 on 2017/3/10. 6 | // Copyright © 2017年 窦心东. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------