如何在不规则形状的框架中拟合图像

时间:2012-07-04 05:05:31

标签: iphone ios uiimageview

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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];
}