
下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
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,_wIDth,_height); UIGraphicsBeginImageContext(CGSizeMake(_wIDth,_height)); //size 为CGSize类型,即你所需要的图片尺寸 [im drawInRect:CGRectMake(0,_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,14.5,6.3,0); } else { CGContextAddArc(ctx,wIDth_/2+0.5,31.3,0); } CGContextFillPath(ctx); self->pickedcolorImageVIEw.image = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext();}//该片段来自于http://outofmemory.cn 以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS从背景图中取色的代码全部内容,希望文章能够帮你解决iOS从背景图中取色的代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)