How to get the pixel color values of custom image inside imageview ios?

前端 未结 4 1231
失恋的感觉
失恋的感觉 2021-01-03 05:59

I know similar question have been asked before.

What I want is to get the RGB pixel value of the Image Inside the Imageview, so it can be any image that

相关标签:
4条回答
  • 2021-01-03 06:08

    You want to know how to get the image from an image view?

    UIImageView has a property, image. Simply use that property.

    0 讨论(0)
  • 2021-01-03 06:16

    Just use this method, it works for me:

    - (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;
    }
    

    Just use like this:

    UIColor *color = [self getPixelColorAtLocation:lastPoint];
    
    0 讨论(0)
  • 2021-01-03 06:18

    If you try to get color from image through CGBitmapContextGetData and set, for example, in background of view, this will be different colors in iPhone 6 and later. In iPhone 5 everything will be ok) More information about this Getting the right colors in your iOS app

    This solution through UIImage give you right color:

    - (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];
    
    }
    
    0 讨论(0)
  • 2021-01-03 06:26

    Use this example article. It is talking about a color picker using images. You can understand required info very easily from it. Helped me in my app. Let me know if any help/suggestion needed ..:)

    EDIT:

    update your getPixelColorAtLocation: like this. It will give you correct color then.

    - (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;
    }
    

    Let me know this fix does not work .. :)

    Here is the GitHub for my code. Use it to implement picker image. Let me know if more info needed

    0 讨论(0)
提交回复
热议问题