国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現從背景圖中取色的代碼

2020-07-26 03:26:48
字體:
來源:轉載
供稿:網友

本文實例講解了iOS從背景圖中取色的代碼,分享給大家供大家參考,具體內容如下

實現代碼:

void *bitmapData; //內存空間的指針,該內存空間的大小等于圖像使用RGB通道所占用的字節數。 static CGContextRef CreateRGBABitmapContext (CGImageRef inImage){  CGContextRef context = NULL;  CGColorSpaceRef colorSpace;  int bitmapByteCount;  int bitmapBytesPerRow;   size_t pixelsWide = CGImageGetWidth(inImage); //獲取橫向的像素點的個數  size_t pixelsHigh = CGImageGetHeight(inImage);   bitmapBytesPerRow  = (pixelsWide * 4); //每一行的像素點占用的字節數,每個像素點的ARGB四個通道各占8個bit(0-255)的空間  bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); //計算整張圖占用的字節數   colorSpace = CGColorSpaceCreateDeviceRGB();//創建依賴于設備的RGB通道  //分配足夠容納圖片字節數的內存空間  bitmapData = malloc( bitmapByteCount );  //創建CoreGraphic的圖形上下文,該上下文描述了bitmaData指向的內存空間需要繪制的圖像的一些繪制參數  context = CGBitmapContextCreate (bitmapData,                   pixelsWide,                   pixelsHigh,                   8,                   bitmapBytesPerRow,                   colorSpace,                   kCGImageAlphaPremultipliedLast);  //Core Foundation中通過含有Create、Alloc的方法名字創建的指針,需要使用CFRelease()函數釋放  CGColorSpaceRelease( colorSpace );  return context;} // 返回一個指針,該指針指向一個數組,數組中的每四個元素都是圖像上的一個像素點的RGBA的數值(0-255),用無符號的char是因為它正好的取值范圍就是0-255static unsigned char *RequestImagePixelData(UIImage *inImage){  CGImageRef img = [inImage CGImage];  CGSize size = [inImage size];  //使用上面的函數創建上下文  CGContextRef cgctx = CreateRGBABitmapContext(img);  CGRect rect = {{0,0},{size.width, size.height}};  //將目標圖像繪制到指定的上下文,實際為上下文內的bitmapData。  CGContextDrawImage(cgctx, rect, img);  unsigned char *data = CGBitmapContextGetData (cgctx);  //釋放上面的函數創建的上下文  CGContextRelease(cgctx);  return data;} //設置背景原圖片,即取色所用的圖片- (void)setSourceImage:(NSString *)sourceImage ImageWidth:(int)_width ImageHeight:(int)_height {  //生成指定大小的背景圖  UIImage *im = [UIImage imageNamed:sourceImage];  UIImage *newImage;  UIImageView *view = [[UIImageView alloc] initWithImage:im];  view.frame = CGRectMake(0, 0, _width, _height);  UIGraphicsBeginImageContext(CGSizeMake(_width, _height)); //size 為CGSize類型,即你所需要的圖片尺寸  [im drawInRect:CGRectMake(0, 0, _width, _height)]; //newImageRect指定了圖片繪制區域  newImage = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();   width = newImage.size.width;  height = newImage.size.height;  //將解析背景圖為像素,供取色用  imgPixel = RequestImagePixelData(newImage);} //計算顏色-(UIColor*)calColor:(CGPoint)aPoint {  int i = 4 * width * round(aPoint.y+imageView.frame.size.height/2) + 4 * round(aPoint.x+imageView.frame.size.width/2);  int _r = (unsigned char)imgPixel[i];  int _g = (unsigned char)imgPixel[i+1];  int _b = (unsigned char)imgPixel[i+2];  NSLog(@"(%f,%f)",aPoint.x,aPoint.y);  NSLog(@"Red : %f  Green: %f  Blue: %f",_r/255.0,_g/255.0,_b/255.0);  return [UIColor colorWithRed:_r/255.0f green:_g/255.0f blue:_b/255.0f alpha:1.0];}   - (void)changColor:(UIColor *)color{  int width_;  if (![Util isIpad]) {    width_ = 30;  } else {    width_ = 70;  }   UIGraphicsBeginImageContext(CGSizeMake(width_, width_));  CGContextRef ctx = UIGraphicsGetCurrentContext();  CGContextMoveToPoint(ctx, 20, 20);  CGContextSetFillColorWithColor(ctx, color.CGColor);  if (![Util isIpad]) {    CGContextAddArc(ctx, width_/2, width_/2, 14.5, 0, 6.3, 0);  } else {    CGContextAddArc(ctx, width_/2+0.5, width_/2, 31.3, 0, 6.3, 0);  }  CGContextFillPath(ctx);  self->pickedColorImageView.image = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();}

以上就是本文的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新河县| 大英县| 织金县| 佛学| 栾城县| 天长市| 水城县| 鄱阳县| 金堂县| 潼关县| 阿拉善左旗| 丽水市| 白城市| 辽阳市| 买车| 太和县| 温州市| 西丰县| 永州市| 阿鲁科尔沁旗| 灵丘县| 新干县| 宣威市| 沧州市| 吉水县| 偃师市| 内江市| 大冶市| 永川市| 房山区| 兰西县| 中阳县| 收藏| 西林县| 水城县| 山东| 托克托县| 濮阳市| 乌兰浩特市| 泽库县| 卓资县|