2016-09-26 199 views
0

我想从IOS UIImage得到面膜(X,Y)的位置和(宽,高)的尺寸,我已经使用UIImageenter image description here 我只想面具从这个UIImage获得形状的框架,我用代码像如何找到的UIView在IOS面膜的位置和大小

thumbImage.layer.mask?.position 
thumbImage.layer.mask?.bounds 

,但我没有得到应有的地位和框架。

有必要使用这种类型的图像掩盖其他图像没有任何其他选项。

请帮助我在UIView中获得正确的框架和面罩位置。

回答

-1

您可以创建变量来获取像这样的代码的大小和位置:CGPoint Position; Position = thumbImage.frame.origin; CGSize Size; Size = thumbImage.frame.size;

+0

我想从掩码动态帧这仅仅是例子,我有很多不同的掩膜图像 –

0

从图像中读取像素,从图像中获取CGRect与设备宽度和高度比较。

- (CGRect)getRectFromImage:(UIImage*)image 
{ 

    // First get the image into your data buffer 
    CGImageRef imageRef = [image CGImage]; 
    NSUInteger width = CGImageGetWidth(imageRef); 
    NSUInteger height = CGImageGetHeight(imageRef); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    unsigned char *rawData = malloc(height * width * 4); 
    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); 


    int x = 0; 
    int y = 0; 
    int xPos = 0; 
    int yPos = 0; 

    int xMax = 0; 
    int yMax = 0; 


    for (x = 0; x < width; x++) { 
     for (y = 0; y < height; y++) { 
      unsigned char alphaByte = rawData[(y*bytesPerRow)+(x*bytesPerPixel)+3]; 
      if (alphaByte > 0) { 

      if (xPos == 0) { 
       xPos = x; 
      } 

      if (yPos == 0) { 
       yPos = y; 
      } 
       if (x < xPos) { 
        xPos = x; 
       } 

       if (y < yPos) { 
        yPos = y; 
       } 

       if (x > xMax) { 
        xMax = x; 
       } 

       if (y > yMax) { 
        yMax = y; 
       } 
      } 
     } 
    } 

    NSLog(@"(%i,%i,%i,%i)", xPos, yPos,xMax-xPos,yMax-yPos); 


    free(rawData); 

    return CGRectMake(xPos, yPos, xMax-xPos, yMax-yPos); 
}