新浦京81707con > 注册购买 > 截图功能和修改图片中的指定颜色,如何截屏

原标题:截图功能和修改图片中的指定颜色,如何截屏

浏览次数:195 时间:2020-04-27

/** * 将图片中的指定颜色转换为透明 */- (UIImage *)imageToTransparentWithOriginalColor:(UIColor *)originalColor { // 分解原来的颜色 CGFloat red = 0.0; CGFloat green = 0.0; CGFloat blue = 0.0; CGFloat alpha = 0.0; [originalColor getRed:&red green:&green blue:&blue alpha:&alpha]; // 分配内存 const int imageWidth = self.size.width; const int imageHeight = self.size.height; size_t bytesPerRow = imageWidth * 4; uint32_t *rgbImageBuf = (uint32_t *)malloc(bytesPerRow * imageHeight); // 创建context CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), self.CGImage); // 遍历像素 int pixelNum = imageWidth * imageHeight; uint32_t* pCurPtr = rgbImageBuf; for (int i = 0; i < pixelNum; i  , pCurPtr  ) { //将像素点转成子节数组来表示---第一个表示透明度即ARGB这种表示方式。ptr[0]:透明度,ptr[1]:R,ptr[2]:G,ptr[3]:B //分别取出RGB值后。进行判断需不需要设成透明。 uint8_t* ptr = (uint8_t *)pCurPtr; // NSLog(@"1是%d,2是%d,3是%d",ptr[1],ptr[2],ptr[3]); if(ptr[1] == red * 255 || ptr[2] == green * 255 || ptr[3] == blue * 255) { ptr[0] = 0; } } // 将内存转成image CGDataProviderRef dataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, nil); CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight,8, 32, bytesPerRow, colorSpace, kCGImageAlphaLast |kCGBitmapByteOrder32Little, dataProvider, NULL, true,kCGRenderingIntentDefault); CGDataProviderRelease(dataProvider); UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef]; // 释放 CGImageRelease; CGContextRelease; CGColorSpaceRelease(colorSpace); return resultUIImage;}/** * 将图片中的指定颜色转换为新的颜色 * * @pram originalColor 原来的颜色 * @pram newColor 新的颜色 */- (UIImage *)imageOriginalColor:(UIColor *)originalColor toNewColor:(UIColor *)newColor { // 分解原来的颜色 CGFloat red1 = 0.0; CGFloat green1 = 0.0; CGFloat blue1 = 0.0; CGFloat alpha1 = 0.0; CGFloat red2 = 0.0; CGFloat green2 = 0.0; CGFloat blue2 = 0.0; CGFloat alpha2 = 0.0; [originalColor getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1]; [newColor getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2]; // 分配内存 const int imageWidth = self.size.width; const int imageHeight = self.size.height; size_t bytesPerRow = imageWidth * 4; uint32_t *rgbImageBuf = (uint32_t *)malloc(bytesPerRow * imageHeight); // 创建context CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), self.CGImage); // 遍历像素 int pixelNum = imageWidth * imageHeight; uint32_t* pCurPtr = rgbImageBuf; for (int i = 0; i < pixelNum; i  , pCurPtr  ) { // 将像素点转成子节数组来表示---第一个表示透明度即ARGB这种表示方式。 // ptr[0]:透明度,ptr[1]:R,ptr[2]:G,ptr[3]:B uint8_t* ptr = (uint8_t *)pCurPtr; if (ptr[0] == alpha1) ptr[0] = alpha2; if (ptr[1] == red1 * 255) ptr[1] = red2 * 255; if (ptr[2] == green1 * 255) ptr[2] = green2 * 255; if (ptr[3] == blue1 * 255) ptr[3] = blue2 * 255; } // 将内存转成image CGDataProviderRef dataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, nil); CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight,8, 32, bytesPerRow, colorSpace, kCGImageAlphaLast |kCGBitmapByteOrder32Little, dataProvider, NULL, true,kCGRenderingIntentDefault); CGDataProviderRelease(dataProvider); UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef]; // 释放 CGImageRelease; CGContextRelease; CGColorSpaceRelease(colorSpace); return resultUIImage;}/** * 全屏截图 */  (UIImage *)screenShot { return [self screenShotWithRect:[UIApplication sharedApplication].keyWindow.bounds isNavigation:NO];}/** * 屏幕截图 * * @pram rect 截图的frame * @pram isNavigation 导航栏是否显示 */  (UIImage *)screenShotWithRect:rect isNavigation:isNavigation { // 获取屏幕放大比例 CGFloat scale = [UIScreen mainScreen].scale; CGRect newRect = rect; newRect.size.width *= scale; newRect.size.height *= scale; CGFloat height = 0; if (isNavigation) { height = NavigationViewHeight; } newRect.origin.y  = height; newRect.origin.x *= scale; newRect.origin.y *= scale; // 设置截屏大小 UIGraphicsBeginImageContextWithOptions(CGSizeMake(newRect.size.width, newRect.size.height), YES, 0); [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = viewImage.CGImage; // 设置截图的区域 CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, newRect); UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect]; // 保存图片到照片库 // UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil); return sendImage;}/** * 返回截取到的图片数据类型 */  dataWithScreenshot { UIImage *image = [self imageWithScreenshot]; UIGraphicsEndImageContext(); return UIImagePNGRepresentation;}/** * 截取当前屏幕 */  (UIImage *)imageWithScreenshot { CGSize imageSize = CGSizeZero; UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsPortrait(orientation)) imageSize = [UIScreen mainScreen].bounds.size; else imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); for (UIWindow *window in [[UIApplication sharedApplication] windows]) { CGContextSaveGState; 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); } else if (orientation == UIInterfaceOrientationLandscapeRight) { CGContextRotateCTM(context, -M_PI_2); CGContextTranslateCTM(context, -imageSize.height, 0); } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { CGContextRotateCTM(context, M_PI); CGContextTranslateCTM(context, -imageSize.width, -imageSize.height); } if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES]; } else { [window.layer renderInContext:context]; } CGContextRestoreGState; } return UIGraphicsGetImageFromCurrentImageContext();}

应用如下:

全屏截图:**Command

一,截图平日供给用到两大作用:全屏截图和钦命区域截图。二,获得部分图纸,必要对图片展开合成的时候,外层图片的底色会很恨恶,大家须要把那层讨厌的水彩去掉,换来透亮的,或然钦点的某种颜色。比方大家代码生成的二维码图片,暗中认可出来就是紫罗兰色底,不过将该二维码和出示集团形象的图纸组合的时候,那张深红底在很理解,最棒是给换来透亮的。

图片 1

  • Shift 3**

      使用快捷键后会立刻截取当前的全屏

    钦命区域截图:Command Shift 4   使用神速键后会出来二个含有座标的瞄准器,用鼠标的拖放能够筛选必要截图的区域

    截图后,图片保存在 桌面 上

全屏截图须要酌量的是现阶段分界面是不是有导航栏,钦定区域截图须求思索的是如今手提式有线电电话机显示器对图纸的缩放比例。更正图片的水彩,其实是遍历该image的每一个像素点,然后找到相应的AHighlanderGB进行值替换。

NSLog(@"nnresult: %@", [Utilities timeIntervalFromLastTime:@"2015年12月8日 15:50"
                                           lastTimeFormat:@"yyyy年MM月dd日 HH:mm"
                                            ToCurrentTime:@"2015/12/08 16:12"
                                        currentTimeFormat:@"yyyy/MM/dd HH:mm"]);

12.对图纸张开滤镜管理

19.减削图片到钦命尺寸大小

//压缩图片到指定尺寸大小
  (UIImage *)compressOriginalImage:(UIImage *)image toSize:(CGSize)size{
    UIImage *resultImage = image;
    UIGraphicsBeginImageContext(size);
    [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIGraphicsEndImageContext();
    return resultImage;
}

输出结果.png

18.截取view中某些区域生成一张图纸

2.获得磁盘可用空间尺寸

//压缩图片到指定文件大小
  (NSData *)compressOriginalImage:(UIImage *)image toMaxDataSizeKBytes:(CGFloat)size{
    NSData *data = UIImageJPEGRepresentation(image, 1.0);
    CGFloat dataKBytes = data.length/1000.0;
    CGFloat maxQuality = 0.9f;
    CGFloat lastData = dataKBytes;
    while (dataKBytes > size && maxQuality > 0.01f) {
        maxQuality = maxQuality - 0.01f;
        data = UIImageJPEGRepresentation(image, maxQuality);
        dataKBytes = data.length/1000.0;
        if (lastData == dataKBytes) {
            break;
        }else{
            lastData = dataKBytes;
        }
    }
    return data;
}
//全屏截图
  (UIImage *)shotScreen{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIGraphicsBeginImageContext(window.bounds.size);
    [window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
//将十六进制颜色转换为 UIColor 对象
  (UIColor *)colorWithHexString:(NSString *)color{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // strip "0X" or "#" if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}
  (CGFloat)diskOfAllSizeMBytes{
    CGFloat size = 0.0;
    NSError *error;
    NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
    if (error) {
#ifdef DEBUG
        NSLog(@"error: %@", error.localizedDescription);
#endif
    }else{
        NSNumber *number = [dic objectForKey:NSFileSystemSize];
        size = [number floatValue]/1024/1024;
    }
    return size;
}

图片 2

11.将十九进制颜色调换为 UIColor 对象

20.滑坡图片到钦定文件大小

7.取得当几天前子

6.将字符串数组依照成分首字母顺序进行排序分组

本文由新浦京81707con发布于注册购买,转载请注明出处:截图功能和修改图片中的指定颜色,如何截屏

关键词: Apple 日记本 功能 截图 Mac

上一篇:ReactiveCocoa系列教程1

下一篇:没有了