美文网首页iOS图像、音频开发iOS移动开发iOS知多少
怎么拿到一张Image的主色调呢?怎样避免imageWithNa

怎么拿到一张Image的主色调呢?怎样避免imageWithNa

作者: Stark_Dylan | 来源:发表于2015-09-11 14:15 被阅读526次

    继续给UIImage加扩展吧、

    • 主色调读取
    
    - (UIColor *)averageColor {
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char rgba[4];
        CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        
        CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage);
        CGColorSpaceRelease(colorSpace);
        CGContextRelease(context);
        
        if(rgba[3] == 0) {
            CGFloat alpha = ((CGFloat)rgba[3])/255.0;
            CGFloat multiplier = alpha/255.0;
            return [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier
                                   green:((CGFloat)rgba[1])*multiplier
                                    blue:((CGFloat)rgba[2])*multiplier
                                   alpha:alpha];
        }
        else {
            return [UIColor colorWithRed:((CGFloat)rgba[0])/255.0
                                   green:((CGFloat)rgba[1])/255.0
                                    blue:((CGFloat)rgba[2])/255.0
                                   alpha:((CGFloat)rgba[3])/255.0];
        }
    }
    
    • UIImage读取
    
    + (UIImage *)imageWithFileName:(NSString *)name {
        NSString *extension = @"png";
        
        NSArray *components = [name componentsSeparatedByString:@"."];
        if ([components count] >= 2) {
            NSUInteger lastIndex = components.count - 1;
            extension = [components objectAtIndex:lastIndex];
            
            name = [name substringToIndex:(name.length-(extension.length+1))];
        }
        
        // 如果为Retina屏幕且存在对应图片,则返回Retina图片,否则查找普通图片
        if ([UIScreen mainScreen].scale == 2.0) {
            name = [name stringByAppendingString:@"@2x"];
            
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
            if (path != nil) {
                return [UIImage imageWithContentsOfFile:path];
            }
        }
        
        if ([UIScreen mainScreen].scale == 3.0) {
            name = [name stringByAppendingString:@"@3x"];
            
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
            if (path != nil) {
                return [UIImage imageWithContentsOfFile:path];
            }
        }
        
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
        if (path) {
            return [UIImage imageWithContentsOfFile:path];
        }
        
        return nil;
    }
    
    • 水印, 文字以及图片
    
    - (UIImage*)imageWaterMarkWithString:(NSString*)str rect:(CGRect)strRect attribute:(NSDictionary *)attri image:(UIImage *)image imageRect:(CGRect)imgRect alpha:(CGFloat)alpha {
        
        UIGraphicsBeginImageContext(self.size);
        [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
        if (image) {
            [image drawInRect:imgRect blendMode:kCGBlendModeNormal alpha:alpha];
        }
        
        if (str) {
            [str drawInRect:strRect withAttributes:attri];
        }
        UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return resultImage;
    }
    

    相关文章

      网友评论

        本文标题:怎么拿到一张Image的主色调呢?怎样避免imageWithNa

        本文链接:https://www.haomeiwen.com/subject/auppcttx.html