2012-07-04 72 views
0

我有关于不规则形状的问题。我搜查了很多,但没有什么对我有用。我有许多不规则形状的框架,每个框架又被分成多个子区域。我想在照片库的每个子区域中放入图片。但我无法获得每个子区域的位置,并且因为形状也不规则,所以再次成为另一个适合该区域图像的问题。谁能帮我 !!这是该框架的一个例子。 Image Having two odd shape frame如何在不规则形状的框架中适合图像

+0

你可以给我们一些关于形状的性质,它是如何产生的,“子区域”的性质等信息? – Rob

+0

认为有圆圈,它分为四个部分。我必须在该区域添加图像。圆的边界和划分为零件的线将成为框架的一部分,框架的所有其他部分将是透明的手段,我必须在该部分适合imageview。 –

回答

0

你永远不会有不规则形状的框架。框架将始终保持原状。 您可以通过检测透明区域来完成。

Refer this link. It will give you idea how to do that :)

+0

我想我误解了你的问题:|你能解释一下确切的情况吗? – DivineDesert

+0

认为有圆圈,它分为四个部分。我必须在该区域添加图像。圆的边界和划分为零件的线将成为框架的一部分,框架的所有其他部分将是透明的手段,我必须在该部分适合imageview。 –

0

你想通过圆弧裁剪的各种图像?例如,这里是一个具有四个图像(只是随机图像我从搜索得到了狗上http://images.google.com)的屏幕快照:

uncropped

这里是由圆裁剪相同的四个图像(或者更准确中,四个图像分别由相同的圆路径裁剪):

cropped

下面是确实的代码

- (UIImage *)cropImage:(UIImage *)image locatedAt:(CGRect)imageFrame byCircleAt:(CGPoint)center withRadius:(float)radius 
{ 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(NULL, imageFrame.size.width, imageFrame.size.height, 8, 4 * imageFrame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst); 

    CGContextBeginPath(context); 
    CGRect ellipseFrame = CGRectMake(center.x - imageFrame.origin.x - radius, imageFrame.size.height - (center.y - imageFrame.origin.y - radius) - radius * 2.0, radius * 2.0, radius * 2.0); 
    CGContextAddEllipseInRect(context, ellipseFrame); 
    CGContextClosePath(context); 
    CGContextClip(context); 

    CGContextDrawImage(context, CGRectMake(0, 0, imageFrame.size.width, imageFrame.size.height), image.CGImage); 

    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    UIImage *newImage = [UIImage imageWithCGImage:imageMasked]; 

    CGImageRelease(imageMasked); 

    return newImage; 
} 

- (void)addSingleCroppedImage:(UIImage *)image at:(CGRect)imageFrame byCircleAt:(CGPoint)center withRadius:(float)radius 
{ 
    UIImage *newImage = [self cropImage:image locatedAt:imageFrame byCircleAt:center withRadius:radius]; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame]; 
    imageView.image = newImage; 
    [self.view addSubview:imageView]; 
} 

- (void)addCroppedImages 
{ 
    NSString *bundlePath = [[NSBundle mainBundle] resourcePath]; 

    CGPoint center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.width/2.0); 
    float radius = 150.0; 

    UIImage *dog1 = [UIImage imageWithContentsOfFile:[bundlePath stringByAppendingPathComponent:@"imgres-1.jpg"]]; 
    UIImage *dog2 = [UIImage imageWithContentsOfFile:[bundlePath stringByAppendingPathComponent:@"imgres-2.jpg"]]; 
    UIImage *dog3 = [UIImage imageWithContentsOfFile:[bundlePath stringByAppendingPathComponent:@"imgres-3.jpg"]]; 
    UIImage *dog4 = [UIImage imageWithContentsOfFile:[bundlePath stringByAppendingPathComponent:@"imgres-4.jpg"]]; 

    CGRect frame; 
    UIImage *currentImage; 

    // upper left 

    currentImage = dog1; 
    frame = CGRectMake(center.x - currentImage.size.width, center.y - currentImage.size.height, currentImage.size.width, currentImage.size.height); 
    [self addSingleCroppedImage:currentImage at:frame byCircleAt:center withRadius:radius]; 

    // upper right 

    currentImage = dog2; 
    frame = CGRectMake(center.x, center.y - currentImage.size.height, currentImage.size.width, currentImage.size.height); 
    [self addSingleCroppedImage:currentImage at:frame byCircleAt:center withRadius:radius]; 

    // lower left 

    currentImage = dog3; 
    frame = CGRectMake(center.x - currentImage.size.width, center.y, currentImage.size.width, currentImage.size.height); 
    [self addSingleCroppedImage:currentImage at:frame byCircleAt:center withRadius:radius]; 

    // lower right 

    currentImage = dog4; 
    frame = CGRectMake(center.x, center.y, currentImage.size.width, currentImage.size.height); 
    [self addSingleCroppedImage:currentImage at:frame byCircleAt:center withRadius:radius]; 
} 
+0

雅亲爱的你明白了我的观点。但是你在四个图像视图中设置图像后裁剪图像,但我的问题是我有一个像你的第二个图像的框架,我必须在这个框架中设置图像。意思是我的框架已经定义好了,我必须在该特定区域中触摸设置图像。 –

+0

所以你只需要一个图像,你想用圆圈裁剪?或者你有一个用于圆形“框架”的现有图形元素(这是一个糟糕的词,因为它意味着在UIView世界中非常不同的东西!),你需要把你的图像放入?不知道我是否理解你所指的这个“框架”是什么样的东西。 – Rob