如何从 UIImagePicker 获取图像的副本
我有一个非常简单的应用程序,用户选择 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西:
Something like this:
使用
但请记住,您需要在 dealloc 时释放该对象
use
But remember that you will need to release the object on dealloc