Comment get les valeurs de couleur de pixel de l'image personnalisée à l'intérieur de ios imageview?

Je sais que des questions similaires ont déjà été posées.

Ce que je veux, c'est get la valeur de pixel RVB de l'image à l'intérieur de l' Imageview , donc ce peut être n'importe quelle image que les valeurs de pixels que nous voulons get.

C'est ce que j'ai utilisé pour get le point où l'image est cliquée.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; if ([touch tapCount] == 2) { //self.imageView.image = nil; return; } CGPoint lastPoint = [touch locationInView:self.imageViewGallery]; NSLog(@"%f",lastPoint.x); NSLog(@"%f",lastPoint.y); } 

Et pour get l'image j'ai collé ce code.

 + (NSArray*)getRGBAsFromImage:(UIImage *)image atX:(int)xx andY:(int)yy count:(int)count { NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; /** It requires to get the image into your data buffer, so how can we get the `ImageViewImage` **/ CGImageRef imageRef = [image CGImage]; NSUInteger width = CGImageGetWidth(imageRef); NSUInteger height = CGImageGetHeight(imageRef); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char)); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); CGContextRelease(context); // Now your rawData contains the image data in the RGBA8888 pixel format. int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; for (int ii = 0 ; ii < count ; ++ii) { CGFloat red = (rawData[byteIndex] * 1.0) / 255.0; CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0; CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0; CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0; byteIndex += 4; UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; [result addObject:acolor]; } free(rawData); return result; } 

Je suis nouveau à ios alors s'il vous plaît expliquer et cela et suggérant un tutoriel sera super.

Utilisez cet exemple d'article . Il s'agit d'un sélecteur de colors utilisant des images. Vous pouvez comprendre très facilement les informations requirejses. M'a aidé dans mon application. Faites-moi savoir si une aide / suggestion nécessaire .. 🙂

MODIFIER:

mettez à jour votre getPixelColorAtLocation: comme ceci. Il te donnera la couleur correcte alors.

 - (UIColor*) getPixelColorAtLocation:(CGPoint)point { UIColor* color = nil; CGImageRef inImage = self.image.CGImage; // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage]; if (cgctx == NULL) { return nil; /* error */ } size_t w = CGImageGetWidth(inImage); size_t h = CGImageGetHeight(inImage); CGRect rect = {{0,0},{w,h}}; /** Extra Added code for Resized Images ****/ float xscale = w / self.frame.size.width; float yscale = h / self.frame.size.height; point.x = point.x * xscale; point.y = point.y * yscale; /** ****************************************/ /** Extra Code Added for Resolution ***********/ CGFloat x = 1.0; if ([self.image respondsToSelector:@selector(scale)]) x = self.image.scale; /*********************************************/ // Draw the image to the bitmap context. Once we draw, the memory // allocated for the context for rendering will then contain the // raw image data in the specified color space. CGContextDrawImage(cgctx, rect, inImage); // Now we can get a pointer to the image data associated with the bitmap // context. unsigned char* data = CGBitmapContextGetData (cgctx); if (data != NULL) { //offset locates the pixel in the data from x,y. //4 for 4 bytes of data per pixel, w is width of one row of data. // int offset = 4*((w*round(point.y))+round(point.x)); int offset = 4*((w*round(point.y))+round(point.x))*x; //Replacement for Resolution int alpha = data[offset]; int red = data[offset+1]; int green = data[offset+2]; int blue = data[offset+3]; NSLog(@"offset: %i colors: RGB A %i %i %i %i",offset,red,green,blue,alpha); color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)]; } // When finished, release the context CGContextRelease(cgctx); // Free image data memory for the context if (data) { free(data); } return color; } 

Faites-moi savoir que cette correction ne fonctionne pas .. 🙂

Voici le GitHub pour mon code. Utilisez-le pour implémenter l'image du sélecteur. Faites-moi savoir si plus d'informations sont nécessaires

Utilisez simplement cette méthode, ça marche pour moi:

 - (UIColor*) getPixelColorAtLocation:(CGPoint)point { UIColor* color = nil; CGImageRef inImage; inImage = imgZoneWheel.image.CGImage; // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage]; if (cgctx == NULL) { return nil; /* error */ } size_t w = CGImageGetWidth(inImage); size_t h = CGImageGetHeight(inImage); CGRect rect = {{0,0},{w,h}}; // Draw the image to the bitmap context. Once we draw, the memory // allocated for the context for rendering will then contain the // raw image data in the specified color space. CGContextDrawImage(cgctx, rect, inImage); // Now we can get a pointer to the image data associated with the bitmap // context. unsigned char* data = CGBitmapContextGetData (cgctx); if (data != NULL) { //offset locates the pixel in the data from x,y. //4 for 4 bytes of data per pixel, w is width of one row of data. int offset = 4*((w*round(point.y))+round(point.x)); alpha = data[offset]; int red = data[offset+1]; int green = data[offset+2]; int blue = data[offset+3]; color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)]; } // When finished, release the context //CGContextRelease(cgctx); // Free image data memory for the context if (data) { free(data); } return color; } 

Utilisez juste comme ceci:

 UIColor *color = [self getPixelColorAtLocation:lastPoint]; 

Si vous essayez d'get la couleur de l'image via CGBitmapContextGetData et que vous la définissez, par exemple, en arrière-plan de la vue, il s'agira de colors différentes dans l'iPhone 6 et les versions ultérieures. Dans l'iPhone 5, tout ira bien) Plus d'informations à ce sujet Obtenir les bonnes colors dans votre application iOS

Cette solution à travers UIImage vous donne la bonne couleur:

 - (UIColor *)getColorFromImage:(UIImage *)image pixelPoint:(CGPoint)pixelPoint { CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(pixelPoint.x, pixelPoint.y, 1.f, 1.f)); UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return [UIColor colorWithPatternImage:croppedImage]; } 

Vous voulez savoir comment get l'image à partir d'une vue d'image?

UIImageView a une propriété, une image. Utilisez simplement cette propriété.