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

首頁(yè) > 系統(tǒng) > iOS > 正文

IOS實(shí)現(xiàn)從背景圖中取色的代碼

2020-02-19 15:49:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

武林技術(shù)頻道的編輯將在本文里實(shí)例講述IOS實(shí)現(xiàn)從背景圖中取色的代碼,目的就是希望能把所學(xué)的知識(shí)點(diǎn)都分享給大家,不廢話了,具體內(nèi)容如下

實(shí)現(xiàn)代碼:

void *bitmapData; //內(nèi)存空間的指針,該內(nèi)存空間的大小等于圖像使用RGB通道所占用的字節(jié)數(shù)。 static CGContextRef CreateRGBABitmapContext (CGImageRef inImage){  CGContextRef context = NULL;  CGColorSpaceRef colorSpace;  int bitmapByteCount;  int bitmapBytesPerRow;   size_t pixelsWide = CGImageGetWidth(inImage); //獲取橫向的像素點(diǎn)的個(gè)數(shù)  size_t pixelsHigh = CGImageGetHeight(inImage);   bitmapBytesPerRow  = (pixelsWide * 4); //每一行的像素點(diǎn)占用的字節(jié)數(shù),每個(gè)像素點(diǎn)的ARGB四個(gè)通道各占8個(gè)bit(0-255)的空間  bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); //計(jì)算整張圖占用的字節(jié)數(shù)   colorSpace = CGColorSpaceCreateDeviceRGB();//創(chuàng)建依賴(lài)于設(shè)備的RGB通道  //分配足夠容納圖片字節(jié)數(shù)的內(nèi)存空間  bitmapData = malloc( bitmapByteCount );  //創(chuàng)建CoreGraphic的圖形上下文,該上下文描述了bitmaData指向的內(nèi)存空間需要繪制的圖像的一些繪制參數(shù)  context = CGBitmapContextCreate (bitmapData,                   pixelsWide,                   pixelsHigh,                   8,                   bitmapBytesPerRow,                   colorSpace,                   kCGImageAlphaPremultipliedLast);  //Core Foundation中通過(guò)含有Create、Alloc的方法名字創(chuàng)建的指針,需要使用CFRelease()函數(shù)釋放  CGColorSpaceRelease( colorSpace );  return context;} // 返回一個(gè)指針,該指針指向一個(gè)數(shù)組,數(shù)組中的每四個(gè)元素都是圖像上的一個(gè)像素點(diǎn)的RGBA的數(shù)值(0-255),用無(wú)符號(hào)的char是因?yàn)樗玫娜≈捣秶褪?-255static unsigned char *RequestImagePixelData(UIImage *inImage){  CGImageRef img = [inImage CGImage];  CGSize size = [inImage size];  //使用上面的函數(shù)創(chuàng)建上下文  CGContextRef cgctx = CreateRGBABitmapContext(img);  CGRect rect = {{0,0},{size.width, size.height}};  //將目標(biāo)圖像繪制到指定的上下文,實(shí)際為上下文內(nèi)的bitmapData。  CGContextDrawImage(cgctx, rect, img);  unsigned char *data = CGBitmapContextGetData (cgctx);  //釋放上面的函數(shù)創(chuàng)建的上下文  CGContextRelease(cgctx);  return data;} //設(shè)置背景原圖片,即取色所用的圖片- (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類(lèi)型,即你所需要的圖片尺寸  [im drawInRect:CGRectMake(0, 0, _width, _height)]; //newImageRect指定了圖片繪制區(qū)域  newImage = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();   width = newImage.size.width;  height = newImage.size.height;  //將解析背景圖為像素,供取色用  imgPixel = RequestImagePixelData(newImage);} //計(jì)算顏色-(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();}

以上就是IOS實(shí)現(xiàn)從背景圖中取色的代碼,你都看懂了嗎?如果還有不清楚的地方可以提出來(lái),武林技術(shù)小編一定竭盡所能。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 海晏县| 剑阁县| 定陶县| 新化县| 凤山县| 云阳县| 长沙市| 大名县| 扶绥县| 县级市| 武隆县| 乐平市| 鄂尔多斯市| 巴里| 关岭| 唐海县| 普安县| 漳平市| 秦皇岛市| 桃园县| 丹江口市| 平乡县| 商水县| 白河县| 南京市| 文昌市| 泌阳县| 遂昌县| 玉林市| 泊头市| 海阳市| 利川市| 洞头县| 稷山县| 布尔津县| 西乡县| 盐边县| 锦州市| 涟源市| 台江县| 太白县|