如何从 UIImagePicker 获取图像的副本

发布于 2024-08-12 15:08:26 字数 1394 浏览 2 评论 0原文

我有一个非常简单的应用程序,用户选择 UIImageView 并按下按钮用相机拍照。然后图像被返回并显示在 UIImageView 中。

但是,由于 UIImageViews 共享同一个委托,因此当其中一个 UIImageViews 中已经有一张图像时,我去拍摄一张照片并将其放置在另一个中,之前的 UIImageView 被替换为空内容(即没有图像)。我想这是因为他们共享同一个代表。有什么方法可以基本上复制图像而不是引用它的委托版本吗?

这是一些示例代码:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker.parentViewController dismissModalViewControllerAnimated:YES];

if (topView == YES)
{
    NSLog(@"topView = %i", topView);
    imageView.image = image;
}
else {
    NSLog(@"topView = %i", topView);
    imageView2.image = image;
}

}

谢谢!

编辑:这是按下按钮时 IBAction 调用的代码

- (IBAction) pushPick {
    topView = YES;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
- (IBAction) pushPick2 {
    topView = NO;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

I've got a very simple app where the user selects a UIImageView and presses a button to take a photo with the camera. The image is then returned and displayed in the UIImageView.

However, since the UIImageViews are sharing the same delegate, when there's already an image in one of the UIImageViews, and I go to take a photo to be placed in another, the previous UIImageView is replaced with empty content (i.e. no image). I guess this is because they're sharing the same delegate. Is there any way I can essentially copy the image instead of referencing the delegate version of it?

Here's some sample code:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker.parentViewController dismissModalViewControllerAnimated:YES];

if (topView == YES)
{
    NSLog(@"topView = %i", topView);
    imageView.image = image;
}
else {
    NSLog(@"topView = %i", topView);
    imageView2.image = image;
}

}

Thanks!

Edit: Here's the code that gets called by the IBAction on the button presses

- (IBAction) pushPick {
    topView = YES;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
- (IBAction) pushPick2 {
    topView = NO;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

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

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

发布评论

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

评论(2

往昔成烟 2024-08-19 15:08:26

像这样的东西:

UIImage * newImage = [UIImage imageWithCGImage:[image CGImage]];

Something like this:

UIImage * newImage = [UIImage imageWithCGImage:[image CGImage]];
陌伤浅笑 2024-08-19 15:08:26

使用

[image copy];

但请记住,您需要在 dealloc 时释放该对象

use

[image copy];

But remember that you will need to release the object on dealloc

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