简体   繁体   English

如何使图像适合不规则形状的框架

[英]How to fit image in irregular shape frame

I have a problem regarding irregular shape. 我对形状不规则有问题。 I searched a lot but nothing was useful for me. 我进行了很多搜索,但对我没有任何帮助。 I have a number of frames of irregular shape and each frame is again divided into sub areas. 我有许多形状不规则的框架,每个框架又被划分为多个子区域。 I want to fit images from photo library in each sub areas of frame. 我想将照片库中的图像放入框架的每个子区域中。 But i am unable to get location of each sub areas and since shape is also irregular so again another problem to fit image in that area. 但是我无法获得每个子区域的位置,并且由于形状也不规则,因此要在该区域中放置图像又是另一个问题。 Can anyone help me !! 谁能帮我 !! This is an example of that frame. 这是该框架的一个示例。 具有两个奇怪形状的图像

You can never have irregular shaped frames. 您永远不会有不规则形状的框架。 Frames will always in rect shape. 框架将始终为矩形。 You can have it done by detecting transparent areas. 您可以通过检测透明区域来完成此操作。

Refer this link. 请参考此链接。 It will give you idea how to do that :) 它将使您知道如何执行此操作:)

Do you want to clip the various images by the arc of the circle? 是否要按圆弧裁剪各种图像? For example, here is a screen snapshot with four images (just random images I got from a search for dogs on http://images.google.com ): 例如,这是一个包含四个图像的屏幕快照(只是我从http://images.google.com上搜索狗获得的随机图像):

未裁剪

And here are the same four images cropped by a circle (or more accurately, each of the four images were separately cropped by the same circle path): 这是用圆圈裁剪的相同的四个图像(或更准确地说,是通过相同的圆圈路径分别裁剪了四个图像):

裁剪

Here is the code that does that 这是执行此操作的代码

- (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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM