iOS實(shí)現(xiàn)從背景圖中取色的代碼
更新時(shí)間:2016年03月02日 11:22:31 投稿:lijiao
這篇文章主要介紹了iOS實(shí)現(xiàn)從背景圖中取色的代碼,感興趣的小伙伴們可以參考一下
本文實(shí)例講解了iOS從背景圖中取色的代碼,分享給大家供大家參考,具體內(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)建依賴于設(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中通過含有Create、Alloc的方法名字創(chuàng)建的指針,需要使用CFRelease()函數(shù)釋放
CGColorSpaceRelease( colorSpace );
return context;
}
// 返回一個(gè)指針,該指針指向一個(gè)數(shù)組,數(shù)組中的每四個(gè)元素都是圖像上的一個(gè)像素點(diǎn)的RGBA的數(shù)值(0-255),用無符號(hào)的char是因?yàn)樗玫娜≈捣秶褪?-255
static 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類型,即你所需要的圖片尺寸
[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();
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
IOS開發(fā)之手勢(shì)響應(yīng)事件優(yōu)先級(jí)的實(shí)例詳解
這篇文章主要介紹了IOS開發(fā)之手勢(shì)響應(yīng)事件優(yōu)先級(jí)的實(shí)例詳解的相關(guān)資料,希望通過本文大家能夠掌握手勢(shì)響應(yīng)優(yōu)先級(jí)的使用方法,需要的朋友可以參考下2017-09-09
iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
iOS TabBarItem設(shè)置紅點(diǎn)(未讀消息)
本文主要介紹了iOS利用TabBarItem設(shè)置紅點(diǎn)(未讀消息)的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-04-04
iOS開發(fā)實(shí)現(xiàn)下載器的基本功能(1)
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)下載器基本功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07

