本文實例講授了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-255
static 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();
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助。
【iOS完成從配景圖中取色的代碼】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!