螢幕小代碼,注意設定高寬和地區,非ARC下注意釋放sendImage
//UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0);[self.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();CGImageRef imageRef = viewImage.CGImage;CGRect rect =CGRectMake(100, 100, 200, 300);//這裡可以設定想要的地區CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];NSData *imageViewData = UIImagePNGRepresentation(sendImage);NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"result.png"]; NSLog(@"%@", savedImagePath);[imageViewData writeToFile:savedImagePath atomically:YES];CGImageRelease(imageRefRect);