我有一个简单的 iPad 图像拼贴应用程序的问题。我有一个 UIImageView,可以将不同的 UIImageView 拖放到其中。拖动的视图可以旋转和拉伸。当用户完成图像编辑后,我会将所有图像合并到最终图像中。我的解决方案是使用 UIGraphics 并绘制大图像,然后遍历所有子视图并在上下文中绘制它们:
- (UIImage*)mergeImages
{
CGSize imageSize = imageView.frame.size;
UIGraphicsBeginImageContext(imageSize);
[imageView.image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
[[imageView subviews] enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
UIImage *imageOriginal = ((UIImageView*)object).image;
CGRect rect = ((UIImageView*)object).frame;
UIImage *imageToAdd = [self scaleImage:imageOriginal :rect.size];
// Find the w/h ratioes
float widthRatio = rect.size.width / imageOriginal.size.width;
float heightRatio = rect.size.height / imageOriginal.size.height;
// Calculate point to draw at
CGPoint pointToDrawAt;
if (widthRatio > heightRatio)
pointToDrawAt = CGPointMake(rect.origin.x + (rect.size.width / 2) - (imageToAdd.size.width / 2), rect.origin.y);
else
pointToDrawAt = CGPointMake(rect.origin.x, rect.origin.y + (rect.size.width / 2) - (imageToAdd.size.height / 2));
[imageToAdd drawAtPoint:pointToDrawAt];
}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save image in photogalery
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
return newImage;
}
只要图像不旋转,上述方法就可以正常工作。所以我的问题是:
有没有办法可以从子视图中找到旋转角度,或者我必须继承 UIView 并使其成为一个属性?还是有另一种方法可以合并图像并获取绘图特定属性,例如角度和滤镜?
如果我可以帮助提供更多代码或任何评论。并提前感谢:)