【问题标题】:Merge and save two UIImageViews合并并保存两个 UIImageViews
【发布时间】:2011-04-08 16:11:47
【问题描述】:

我的项目有两个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 很陌生,所以我不知道如何解决这个问题。

非常感谢!

【问题讨论】:

    标签: objective-c cocoa-touch ios uiimageview


    【解决方案1】:

    你可以这样做。

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

    然后在你的按钮动作中像

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

    【讨论】:

      【解决方案2】:
        `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:' 方法....

      【讨论】:

      • 感谢您回答我的问题。我试图用您发布的代码替换“captureScreen”代码。但是当我按下按钮时,应用程序似乎崩溃了。再次感谢!
      • 在顶部代码(-UIImage...):“'imageView'的本地声明隐藏实例变量。” “'Birdie' 的本地声明隐藏了实例变量。”在 buttonvoid 的部分:“从不同的 Objective-C 类型传递 'addImage:toImage:' 的参数 1 时,不兼容的 Objective-C 类型'struct UIImageView *',预期的'struct UIImage *'”。提前致谢!
      • Tim,你不能像这样使用 UIImage.... 你必须使用 UIImageView.... 1.创建一个 UIImageView 对象....使用 initWithFrame: 方法 2. 创建 UIImage obj我之前提到的方法 3. 在 UIImageView 对象上设置此图像 4. 在您​​的视图上添加此 UIImageView obj 5. 创建另一个 UIImageView 对象,但使用差异框架,这样两个 UIImageView 对象不会重叠 6. 做其他事情:创建 UIImage obj 和将其设置为此 UIImageView obj 现在在您的视图上添加第二个 UIImageView。之后你可以 GetGraphic 上下文并使用它
      • 你也对 UIImage 对象使用了错误的方法.....[drawRect]......这是崩溃的原因......
      • 感谢您的评论,但我只是一个菜鸟,所以我不明白整个事情。我必须用什么替换 drawRect 以使其不会崩溃并且整个工作正常?提前致谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      相关资源
      最近更新 更多