保存两个重叠的UIImage

时间:2011-06-12 07:59:13

标签: iphone cocoa-touch uiimage

我正在尝试在iphone上构建相框应用程序。我在png formate中制作了透明的框架,然后通过选择照片并将其放置在界面构建器中的框架层后面。

在界面构建器中,它们放置得很好并且很合适。现在我的问题是如何将它们保存为一张图片。

这是我的代码,但保存部分一直在崩溃。

-(IBAction) saveImage:(id)sender{

    imagefront .backgroundColor = [UIColor clearColor]; //This sets your backgroung to transparent. 
    imagefront.opaque = NO;
    [imageView bringSubviewToFront:imagefront];

    UIImage *overlappedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    UIImageWriteToSavedPhotosAlbum(overlappedImage, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}

Imagefront是相框,而imageView是照片。

谢谢。

1 个答案:

答案 0 :(得分:1)

您当前的方法不正确。您需要这样做才能获得图像。

UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsGetCurrentContext();

这假设imageView已将imageFront作为其子视图,如您发布的代码所示。