合并并保存两个 UIImageView

发布于 2024-10-31 02:54:51 字数 816 浏览 3 评论 0原文

我的项目有两个 UIImages,一个名为 imageView,另一个名为 Birdie

- (UIImage *)addImage:(UIImage *)imageView toImage:(UIImage *)Birdie {
    UIGraphicsBeginImageContext(imageView.size);

    // Draw image1
    [imageView drawInRect:CGRectMake(0, 0, imageView.size.width, imageView.size.height)];

    // Draw image2
    [Birdie drawInRect:CGRectMake(0, 0, Birdie.size.width, Birdie.size.height)];

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return resultingImage;
}

-(void)captureScreen{
    UIImageWriteToSavedPhotosAlbum(resultingImage, nil, nil, nil);
}

这是我的代码,captureScreen按钮是保存图像的。它不起作用,因为 resultingImage 未声明,但我对 Xcode 还很陌生,所以我不知道如何解决这个问题。

非常感谢!

My project has two UIImages, one named imageView and one named Birdie.

- (UIImage *)addImage:(UIImage *)imageView toImage:(UIImage *)Birdie {
    UIGraphicsBeginImageContext(imageView.size);

    // Draw image1
    [imageView drawInRect:CGRectMake(0, 0, imageView.size.width, imageView.size.height)];

    // Draw image2
    [Birdie drawInRect:CGRectMake(0, 0, Birdie.size.width, Birdie.size.height)];

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return resultingImage;
}

-(void)captureScreen{
    UIImageWriteToSavedPhotosAlbum(resultingImage, nil, nil, nil);
}

This is my code, the captureScreen button is to save the image. It's not working because resultingImage is undeclared, but I'm pretty new to Xcode so I don't know how to fix this.

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷又爱笑 2024-11-07 02:54:52
  `resultingImage` 

不是全局变量,其范围仅限于 - (UIImage *)addImage:(UIImage *)imageView toImage:(UIImage *)Birdie 方法,您必须调用此方法并使用此方法返回的对象方法。

-(void)captureScreen{
     UIImage *resultingImage = [self addImage:yourFirstImage toImage:yourSecondImage];
    UIImageWriteToSavedPhotosAlbum(resultingImage, nil, nil, nil);
}

蒂姆,
您应该记住一点,我们不能直接调用“drawRect”方法。
系统会调用它。
所以这里你应该使用 UIImage 可用的其他方法。
您可以使用“imageNamed:”方法......

  `resultingImage` 

Is not a global variable its scope is limited to only - (UIImage *)addImage:(UIImage *)imageView toImage:(UIImage *)Birdie method you have to call this method and use returned object by this method.

-(void)captureScreen{
     UIImage *resultingImage = [self addImage:yourFirstImage toImage:yourSecondImage];
    UIImageWriteToSavedPhotosAlbum(resultingImage, nil, nil, nil);
}

Tim,
one point you should remember tht we must not call 'drawRect' method directly.
It would be called by the system.
So here you should use other methods available for UIImage.
you can use 'imageNamed:' method....

苏大泽ㄣ 2024-11-07 02:54:51

你可以做这样的事情。

将此 -(void)captureScreen: 方法覆盖为 -(void)captureScreen:(UIImage*)imageToSave

然后在您的按钮操作中,例如

- (IBAction)myButton: (id)发件人{
[self captureScreen:[self addImage:imageViewImage toImage:BirdieImage]]
}

You can do something like this.

Override this -(void)captureScreen: method to -(void)captureScreen:(UIImage*)imageToSave

then in your button action like

- (IBAction)myButton:(id)sender{
[self captureScreen:[self addImage:imageViewImage toImage:BirdieImage]]
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文