1

不規則な形状に問題があります。私はたくさん検索しましたが、何も役に立ちませんでした。不規則な形状のフレームが多数あり、各フレームは再びサブエリアに分割されています。フレームの各サブ領域にフォト ライブラリの画像を合わせたい。しかし、各サブエリアの位置を取得できず、形状も不規則であるため、そのエリアに画像を収めるには別の問題があります。誰でも私を助けることができます!! そのフレームの一例です。異形のフレームが2つある画像

4

2 に答える 2

0

さまざまな画像を円の弧でクリップしますか?たとえば、次の4つの画像を含む画面スナップショットです(http://images.google.comで犬を検索して取得したランダムな画像のみ)。

トリミングされていない

そして、これが円によってトリミングされた同じ4つの画像です(より正確には、4つの画像のそれぞれが同じ円のパスによって別々にトリミングされました):

トリミング

これがそれを行うコードです

- (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];
}
于 2012-07-04T08:23:06.623 に答える
0

不規則な形のフレームを持つことはできません。フレームは常に四角形になります。透明な領域を検出することで実行できます。

このリンクを参照してください。それはあなたにそれを行う方法を教えてくれます:)

于 2012-07-04T05:16:09.570 に答える