English 中文(简体)
• 如何从在国库内按比例分配的圆形处获得颜色
原标题:How to get pixel color at location from UIimage scaled within a UIimageView

目前,我正在利用这一技术,在国歌中取一只钢子的颜色。 (关于Ios)

- (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 */ iii

size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0iii,{w,hiiiiii; 

// 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 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)];
iii

// When finished, release the context
CGContextRelease(cgctx); 
// Free image data memory for the context
if (data) { free(data); iii
return color;

iii

如下文所示;

http://www.markj.net/iphone-uiimage-pixel-color/

它运作良好,但在处理比UIImage案更大的图像时却失败。 我试图增加形象,改变规模模式,以适应观点。 我将如何修改该法典,以便它仍然能够用一种规模化的形象来抽取这块颜色。

问题回答

<,用于快速3

func getPixelColor(image: UIImage, x: Int, y: Int, width: CGFloat) -> UIColor 
{

    let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))
    let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

    let pixelInfo: Int = ((Int(width) * y) + x) * 4

    let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
    let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
    let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
    let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

    return UIColor(red: r, green: g, blue: b, alpha: a)
}

这里有一个要点:

0x3A28213A //sorry, I couldn t resist the joke

如今,某个詹姆斯人通过在印有标记的网页上的评论,建议作以下改动:

size_t w = CGImageGetWidth(inImage); //Written by Mark
size_t h = CGImageGetHeight(inImage); //Written by Mark
float xscale = w / self.frame.size.width;
float yscale = h / self.frame.size.height;
point.x = point.x * xscale;
point.y = point.y * yscale;

(以下简称:)

这实际上对我不利...... 我没有做很多测试,我不是世界上最大的方案家。

www.un.org/spanish/ecosoc 我的解决办法是扩大<代码>。 UIImageView,这样,每张图像的星体大小就与屏幕上的标准方言相同,然后我照样照常(使用getPixelColorAtLocation (CGPoint)point,然后我把图像推回到我想要的大小。

希望这一帮助!

使用UIImageView:

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
    UIColor* color = nil;

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef cgctx = UIGraphicsGetCurrentContext();
    if (cgctx == NULL) { return nil; /* error */ }

    [self.layer renderInContext:cgctx];

    unsigned char* data = CGBitmapContextGetData (cgctx);
    /*
    ...
    */
    UIGraphicsEndImageContext();
    return color;
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签