iphone - iOS:UIImageView 示例代码

标签 iphone ios xcode uiimageview

我正在编写一个应用程序,允许用户从他们的相机胶卷上传照片并将它们保存在应用程序中。唯一的问题是我不确定从哪里开始。我阅读了有关 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/

相关文章:

ios - 我如何查看 XCode 引用的浏览器版本?想查看方法列表

c++ - Xcode 与 Visual Studio : Difference in runtime for compiled C++ programs

iphone - UITextView - 水平滚动?

ios - iphone 应用程序不在 ipad 上显示图标

iphone - 一些 xcode 项目中的 xx.bundle 目录

ios - Xcode 堆栈跟踪未出现在控制台中

iphone - 检测自定义按钮的 UIAppearance 代理中的更改

ios - MYSQL 和 Swift - 上传图像和文件 ||使用 Alamofire 会更好吗?

ios - writeToFile 不适用于保存照片

xcode - ModuleMap路径解析