gpt4 book ai didi

iphone - iOS:UIImageView 示例代码

转载 作者:行者123 更新时间:2023-11-29 11:16:55 25 4
gpt4 key购买 nike

我正在编写一个应用程序,允许用户从他们的相机胶卷上传照片并将它们保存在应用程序中。唯一的问题是我不确定从哪里开始。我阅读了有关 UIImageView 的 Apple 文档,但这似乎并没有像往常那样有帮助。我想知道是否有人可以向我指出一些通常做同样事情的示例代码的正确方向?

谢谢!

最佳答案

好吧,让我们先把事情弄清楚

  • UIImageView 是一个用于显示 UIImage 的可视化组件(无论它们来自哪里)
  • UIImagePickerController 是 Apple 提供的组件,用于获取图片(来自用户相册或直接来自相机)
  • 保存图像意味着将其存储在本地文件系统中

现在让我们看看它是如何组合在一起的:

首先,您需要创建一个 UIImagePickerController,定义图像的来源并设置一个委托(delegate)来处理返回的图像。沿着这些线的东西:

// Always make sure to test if the source you want is available
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;

//Our image picker is ready - now let's show it to the user
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];

}

这将为用户打开图像选择器(特别是相机 - 您可以使用媒体类型和来源 - 查找 UIImagePickerController Reference 以查看您还有哪些其他选项。

现在 - 您必须真正处理从用户那里获得的图像,这是通过委托(delegate)方法实现完成的:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//All the info from the image - including the image itself - are stored in the info dictionary.

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

//Now - we have a reference to the image as a UIImage, so we can display it in a UIImageView or save it to disk or upload to a webserver. To show it - just set your imageView's image to the image
imageView.image = image;
}
}

关于iphone - iOS:UIImageView 示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9041897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com