如何使用特定名称保存 UIImagePicker 图像

发布于 2024-08-18 01:22:54 字数 131 浏览 1 评论 0原文

我使用 UIPickerController 从 iphone 捕获图像,然后如何以编程方式以特定名称保存它,并稍后调用图像(通过使用名称),是否有可能更改图像大小,例如。从 100 x 100 像素到 50 x 50 像素,

谢谢

i capture image from iphone using UIPickerController, then how programatically to save it with specific name, and to call the image later (by using the name), is it any possibilities to change the image size eg. from 100 x 100 pixels to 50 x 50 pixels

thanks

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

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

发布评论

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

评论(1

屋顶上的小猫咪 2024-08-25 01:22:54

以下代码片段将图像保存到文件中:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *anImage = [info valueForKey:UIImagePickerControllerOriginalImage];
    NSData *imageData = UIImageJPEGRepresentation(anImage, 1.0);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *tmpPathToFile = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/specificImagedName.jpg", path]];

    if([imageData writeToFile:tmpPathToFile atomically:YES]){
          //Write was successful. 
    }
}

然后从文件中调用图像:

NSData *imageData = [NSData dataWithContentsOfFile:tmpPathToFile];
[UIImage imageWithData:imageData];

最后, 这篇文章提供了一种方法,您可以将其合并到您的项目中以调整图像大小。

The following code snippet will save the image to a file:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *anImage = [info valueForKey:UIImagePickerControllerOriginalImage];
    NSData *imageData = UIImageJPEGRepresentation(anImage, 1.0);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *tmpPathToFile = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/specificImagedName.jpg", path]];

    if([imageData writeToFile:tmpPathToFile atomically:YES]){
          //Write was successful. 
    }
}

And then to recall the image from a file:

NSData *imageData = [NSData dataWithContentsOfFile:tmpPathToFile];
[UIImage imageWithData:imageData];

Finally, this post has a method available that you can incorporate into your project to resize the image.

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