使用BezierPath剪切图像并在剪切后组合图像

时间:2014-07-19 10:45:34

标签: ios uiimageview uiimage uibezierpath

我想开发一个小型拼图益智游戏,但在组合图像时遇到问题。我可以分割图像但不能按照我的要求组合它们。这就是我在做的事情。

用于裁剪:

[customImageView setImage:[self cropImage:self.mainImage withRect:mCropFrame]];


- (UIImage *) cropImage:(UIImage*)originalImage withRect:(CGRect)rect
{
    return [UIImage imageWithCGImage:CGImageCreateWithImageInRect([originalImage CGImage], rect)];
}

对于裁剪:

[self setClippingPath:[pieceBezierPathsMutArray_ objectAtIndex:i]:view];


- (UIImageView *) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{
    if (![[imgView layer] mask])
    {
        [[imgView layer] setMask:[CAShapeLayer layer]];
    }

    [(CAShapeLayer*) [[imgView layer] mask] setPath:[clippingPath CGPath]];
    return imgView;
}

For Combining:

-(id)initByCombining:(id)oneView andOther:(id)twoView withRegularSize:(CGSize)pieceSize;
{
    CustomImageView *one = oneView;//[oneView copy]; 
    CustomImageView *two = twoView;

    CGPoint onepoint, twopoint;
    if (one.frame.origin.x < two.frame.origin.x)
    {
        onepoint.x = 0;
        twopoint.x = onepoint.x + one.frame.size.width;
    }
    else
    {
        onepoint.x = onepoint.x + one.frame.size.width;
        twopoint.x = 0;
    }
    if (one.frame.origin.y < two.frame.origin.y)
    {
        onepoint.y = 0;
        twopoint.y = 0;
    }
    else
    {
        onepoint.y = 0;
        twopoint.y = 0;
    }


    CGRect frame;
    frame.origin      = CGPointZero;
    frame.size.width  = onepoint.x + one.frame.size.width + two.frame.size.width;
    frame.size.height = MAX(one.frame.size.height , two.frame.size.height);

    if (self = [self initWithFrame:frame])
    {
        UIGraphicsPushContext(UIGraphicsGetCurrentContext());
        UIGraphicsBeginImageContext(frame.size);

        [one.image drawAtPoint:onepoint];
        [two.image drawAtPoint:twopoint];

        [self.layer renderInContext:UIGraphicsGetCurrentContext()];

        self.image = UIGraphicsGetImageFromCurrentImageContext();
        self.backgroundColor = [UIColor redColor];

        UIGraphicsEndImageContext();
        UIGraphicsPopContext();
        self.center = one.center;
        self.transform = CGAffineTransformScale(incomingTransform, 0.5, 0.5);
        self.previousRotation = self.transform;
    }
    return self;
}

我的初步形象是这样的:

裁剪和裁剪后,它变成如下:

合并后应该是这样的。

但它变得像这样。

1 个答案:

答案 0 :(得分:0)

当你想要组合图像时,我建议你将剪辑放在另一个UIView中,这样UIView就会成为所有Placed Clippings的SuperView,在放置图像之后你可以做这样的事情,

UIGraphicsBeginImageContextWithOptions(superView.bounds.size, superView.opaque, 0.0);

[superView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * CombinedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return CombinedImage;

然后将其保存如下..

ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];

    [library writeImageToSavedPhotosAlbum:[CombinedImage CGImage] orientation:(ALAssetOrientation)[SAVEIMAGE imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) {
            // TODO: error handling
            UIAlertView *al=[[UIAlertView alloc]initWithTitle:@"" message:@"Error saving image, Please try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

            [al show];

        } else {
            NSData *imageData = UIImagePNGRepresentation(CombinedImage);
           UIImage *finalImage=[UIImage imageWithData:imageData];

}

..希望这有帮助