1

Have the similar problem as this post: How to remove the transparent area of an UIImageView after masking?

but solution doesn't help me, in my case I am cropping image to multi parts, and every part has bounds like parent image had, this is cropping function:

- (UIImage*) maskImage:(CALayer *) sourceLayer toAreaInsidePath:(UIBezierPath*) maskPath;
{
    return [self compositeImage:sourceLayer onPath:maskPath usingBlendMode:kCGBlendModeSourceIn];
}

- (UIImage*) compositeImage:(CALayer *) layer onPath:(UIBezierPath*) path usingBlendMode:(CGBlendMode) blend;
{
    UIGraphicsBeginImageContext([layer frame].size);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIGraphicsBeginImageContext([layer frame].size);
    [path fill];
    [sourceImage drawInRect:[layer frame] blendMode:blend alpha:1.0];
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return maskedImage;
}

and how I am using it:

- (PuzzlePart *)calculate:(NSDictionary *) item
{
    UIBezierPath *path = [self calculatePath:[item objectForKey:@"figure"]];
    UIImageView *iView = [[UIImageView alloc] initWithImage:image];
    //iView.contentMode = UIViewContentModeCenter;
    UIImage *imagePart = [self maskImage:iView.layer toAreaInsidePath:path];
    iView.image = imagePart;
    return [[PuzzlePart alloc] initWithParams:imagePart lhckID:lifehackID];
}

- (UIBezierPath *)calculatePath:(NSArray *) points {
    UIBezierPath *path = [UIBezierPath bezierPath];
    NSDictionary *point = [points objectAtIndex:0];
    [path moveToPoint:CGPointMake([point[@"x"] intValue], [point[@"y"] intValue])];
    for (int i = 1; i < [points count]; i++) {
        point = [points objectAtIndex:i];
        [path addLineToPoint:CGPointMake([point[@"x"] intValue], [point[@"y"] intValue])];
    }
    [path closePath];
    return path;
}

and place it:

- (void)placeItems
{
    for (PuzzlePart *part in puzzleParts) {
        UIImage *imagePart = [part imagePart];
        CGRect newRect = [self cropRectForImage:imagePart];
        UIImage *image = [self imageWithImage:imagePart convertToSize:newRect.size];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        //imageView.contentMode = UIViewContentModeCenter;
        imageView.center = CGPointMake([self getRandomNumberBetween:0 to:self.view.frame.size.width],
                                       [self getRandomNumberBetween:0 to:self.view.frame.size.height]);
        [self.view addSubview:imageView];
        while ([self viewIntersectsWithAnotherView:self.view chosenView:imageView]) {
            viewPositionMovingStep++;
            [self placeItem:self.view:imageView];
        }
    }
    [self startCountdown];
}

[self cropRectForImage:imagePart] is the same method from post I marked and this what I have after:

enter image description here

any ideas? Thank you!

Community
  • 1
  • 1
Eugene
  • 145
  • 1
  • 10

0 Answers0