Ios UIImage获取整数数组中图像的像素
我想在ios中获取图像的像素 我知道如何在安卓系统中做到这一点,我正在寻找与ios相同的方法 http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels(int[],int,int,int,int,int,int,int,int,int) 我发现了一堆ios的示例代码,但没有一个像android框架那样干净。因此,我猜想有一种更好、更简单的方法,主要由框架来处理。Ios UIImage获取整数数组中图像的像素,ios,uiimage,core-graphics,Ios,Uiimage,Core Graphics,我想在ios中获取图像的像素 我知道如何在安卓系统中做到这一点,我正在寻找与ios相同的方法 http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels(int[],int,int,int,int,int,int,int,int,int) 我发现了一堆ios的示例代码,但没有一个像android框架那样干净。因此,我猜想有一种更好、更简单的方法,主要由框架来处理。-(NSArray*)getPixel
-(NSArray*)getPixelsFromImage:(UIImage*)image
-(NSArray*)getPixelsFromImage:(UIImage*)image
{
NSMutableArray *pixelData=[NSMutableArray new];
for (int y = 0; y<image.size.height; y+=1) {
for (int x = 0; x<image.size.width; x+=1) {
CGImageRef tmp = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(x, y, 1, 1));
UIImage *part=[UIImage imageWithCGImage:tmp];
[pixelData addObject:part];
}
}
return [pixelData copy];
}
{
NSMUTABLEARRY*像素数据=[NSMUTABLEARRY new];
对于(inty=0;y
- (void) imagePixel:(UIImage *)image
{
struct pixel {
unsigned char r, g, b, a;
};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = image.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
struct pixel *pixels = (struct pixel *) calloc(1, sizeof(struct pixel) * width * height);
size_t bytesPerComponent = 8;
size_t bytesPerRow = width * sizeof(struct pixel);
CGContextRef context = CGBitmapContextCreate(pixels, width, height, bytesPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
unsigned long numberOfPixels = width * height;
if (context != NULL) {
for (unsigned i = 0; i < numberOfPixels; i++) {
//you can add code here
pixels[i].r;
pixels[i].g;
pixels[i].b;
}
free(pixels);
CGContextRelease(context);
}
CGColorSpaceRelease(colorSpace);
}
最后,您应该使用
CGImageRelease(imageR)
这不是太占用内存了吗?这很有效,谢谢。你有没有一个好方法可以从这些像素重新创建图像?
CGImageRelease(imageR)