2012-07-04 70 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