标签:xsl 常见 xtu ie8 create img let car vpd
CGContextRef contexRef = CGBitmapContextCreate(void *data, size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow, CGColorSpaceRef space, uint32_t bitmapInfo);
unsigned char* needData = malloc(需要多大); needData = CGBitmapContextGetData(contexRef);
int alpha = needData[4*n]; int red = needData[4*n+1]; int green = needData[4*n+2]; int blue = needData[4*n+3];
newData[4*n ] = alpha; newData[4*n + 1] = red; newData[4*n + 2] = green; newData[4*n + 3] = blue;
CGImageRef cgImage = CGBitmapContextCreateImage(newContext); _imageView.image = [UIImage imageWithCGImage:cgImage ];
所有的图形点由老的x y坐标变成了新的x` y`坐标
1 -(void)changeImageByPoints:(NSArray *)pointArray{ 2 UIImage * image = _image; //全局需要变换的图片 3 float width = CGImageGetWidth(image.CGImage); 4 float height = CGImageGetHeight(image.CGImage); 5 6 CGPoint p0 = [pointArray[0]CGPointValue]; 7 CGPoint p1 = [pointArray[1]CGPointValue]; 8 CGPoint p2 = [pointArray[2]CGPointValue]; 9 CGPoint p3 = [pointArray[3]CGPointValue]; 10 11 //痛觉相对于父视图的绝对4个顶点计算出新的宽度和高度 12 float minLeft = MIN(MIN(p0.x, p1.x), MIN(p2.x, p3.x)); 13 float minTop = MIN(MIN(p0.y, p1.y), MIN(p2.y, p3.y)); 14 float shapW = KINT((MAX(MAX(p0.x, p1.x), MAX(p2.x, p3.x)) - minLeft)); 15 float shapH = KINT((MAX(MAX(p0.y, p1.y), MAX(p2.y, p3.y)) - minTop)); 16 17 //change point relative to image not superview 18 p0.x = p0.x - minLeft; 19 p1.x = p1.x - minLeft; 20 p2.x = p2.x - minLeft; 21 p3.x = p3.x - minLeft; 22 p0.y = p0.y - minTop; 23 p1.y = p1.y - minTop; 24 p2.y = p2.y - minTop; 25 p3.y = p3.y - minTop; 26 27 //创建一个bitmapcontext 28 if (!_first) { 29 needData = malloc(KINT(width)* KINT(height) * 4); 30 CGContextRef imageContext = CGBitmapContextCreate(needData, width, height, 8, width * 4, CGImageGetColorSpace(image.CGImage), CGImageGetAlphaInfo(image.CGImage)); 31 CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image.CGImage); 32 data = malloc(KINT(width) * KINT(height) * 4); 33 data = CGBitmapContextGetData(imageContext); 34 _first = YES; 35 } 36 37 //初始化新的图片需要的data 38 unsigned char* shapeData = malloc(shapW * shapH * 4); 39 for (int i = 0; i < shapH -1; i ++) { 40 for (int j = 0; j < shapW -1; j++) { 41 int offset = (i * shapW + j) * 4; 42 shapeData[offset] = 255; 43 shapeData[offset + 1] = 255; 44 shapeData[offset + 2] = 255; 45 shapeData[offset + 3] = 255; 46 } 47 } 48 49 //给data添加对应的像素值 50 for (int i = 0; i < height -1; i++) { 51 for (int j = 0; j < width -1; j++) { 52 CGPoint originPoint = CGPointMake(j, i); 53 int originOffset = (i * width + j) * 4; 54 // 计算原图每个点在新图中的位置 55 float xFunc = (float)originPoint.x / (float)width; 56 float yFunc = (float)originPoint.y / (float)height; 57 58 float delx = (p1.x - p0.x) * xFunc; 59 float dely = (p1.y - p0.y) * xFunc; 60 CGPoint topPoint = CGPointMake(p0.x + delx, p0.y + dely); 61 62 delx = (p2.x - p3.x) * xFunc; 63 dely = (p2.y - p3.y) * xFunc; 64 CGPoint bottomPoint = CGPointMake(p3.x + delx, p3.y + dely); 65 66 delx = (bottomPoint.x - topPoint.x) * yFunc; 67 dely = (bottomPoint.y - topPoint.y) * yFunc; 68 69 CGPoint newPoint = CGPointMake(topPoint.x + delx, topPoint.y + dely); 70 71 int newOffset = ((KINT(newPoint.y) * shapW + KINT(newPoint.x))) * 4; 72 73 //give shapeView new value 74 shapeData[newOffset] = data[originOffset]; 75 shapeData[newOffset + 1] = data[originOffset + 1]; 76 shapeData[newOffset + 2] = data[originOffset + 2]; 77 shapeData[newOffset + 3] = data[originOffset + 3]; 78 79 } 80 } 81 //创建新图片 82 CGContextRef newContext = CGBitmapContextCreate(shapeData, shapW, shapH, 8, shapW * 4, CGImageGetColorSpace(image.CGImage), CGImageGetAlphaInfo(image.CGImage)); 83 84 CGImageRef cgImage = CGBitmapContextCreateImage(newContext); 85 _imageView.image = [UIImage imageWithCGImage:cgImage ]; //这个_imageView就是贴上viewcontroller上面的UIImageview 86 _imageView.frame = CGRectMake(minLeft, minTop, shapW, shapH); 87 CGContextRelease(newContext); 88 CGImageRelease(cgImage); 89 free(shapeData); 90 }
标签:xsl 常见 xtu ie8 create img let car vpd
原文地址:http://www.cnblogs.com/xiaolong-/p/7815991.html