2

I have a UIView that contains some subviews - drawn using UIBezierPath. Every subview contains a scrollview having image as mask. When I try to render image of whole view (overall), instead of making image from path, it draws image of individual rect of subviews. Let me make it clear by figure:

Original View:

enter image description here

What I am getting after rendering Image:

enter image description here

I don't know what am I missing. Please guide me. Thanks.

Code:

IrregularSquare

@interface IrregularSquare : UIView
{
UIScrollView *imageResizer;
CAShapeLayer *maskLayer;
}
@end

@implementation IrregularSquare

- (id)initWithFrame:(CGRect)frame index:(NSInteger) index
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.layer.backgroundColor = [UIColor clearColor].CGColor;

    }
    return self;
}

-(void) drawRect:(CGRect)rect
{
    UIBezierPath *aPath = [UIBezierPath bezierPath];

            [aPath moveToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect))];
            [aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect))];
            [aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect) - 20, CGRectGetMidY(rect) - 10)];
            [aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect) - 5, CGRectGetMaxY(rect) - 10)];
            [aPath addLineToPoint:CGPointMake(CGRectGetMidX(rect) + 10, CGRectGetMaxY(rect))];
            [aPath addLineToPoint:CGPointMake(CGRectGetMidX(rect) - 20, CGRectGetMaxY(rect) - 20)];
            [aPath addLineToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect) - 20)];
    [aPath closePath];
    aPath.lineWidth = 2;
    [[UIColor whiteColor] setStroke];
    [aPath stroke];
    [self initializeImageWithPath]; //this method adds scrollview to main view and assigns an image to scrollview with mask
}

-(void) initializeImageWithPath
{
    CGRect aFrame = somevalue;

    self.backImage = [[UIImageView alloc] initWithFrame:aFrame];
    self.backImage.contentMode = UIViewContentModeScaleAspectFill;
    self.backImage.clipsToBounds = YES;

    imageResizer = [[UIScrollView alloc] initWithFrame:self.bounds];
    imageResizer.showsHorizontalScrollIndicator = imageResizer.showsVerticalScrollIndicator = NO;
    imageResizer.scrollEnabled = YES;
    imageResizer.backgroundColor = [UIColor clearColor];
    imageResizer.zoomScale = 1.0;
    imageResizer.delegate = self;
    imageResizer.clipsToBounds = YES;
    [imageResizer addSubview:self.backImage];
    [self addSubview: imageResizer];

    maskLayer = [CAShapeLayer layer];
    maskLayer.path = [aPath CGPath];
    self.layer.mask = maskLayer;

}

IrregularShapesContainer.m

@implementation IrregularShapesContainer

-(void) drawSubviews
{
    for(UIView *aView in self.subviews)
        [aView removeFromSuperview];

    IrregularSquare *sq1 = [[IrregularSquare alloc] initWithFrame:CGRectMake(HORIPADDING, VERTICALPADDING, (self.frame.size.width - (HORIPADDING * 2) - (SPACING * 2)) * .5, (self.frame.size.height - (VERTICALPADDING) - SPACING) * .5) index:1];
    [self addSubview:sq1];
    [sq1.layer display];
    [self randomlyPlaceImage:sq1];
    sq1.irrViewDelegate = self;

//many similar instances are added in following code
}

@end

I hope my Question is clear.. Any help is appreciated.

NightFury
  • 13,436
  • 6
  • 71
  • 120

0 Answers0