ios - 在保存到目录之前在 UIImage 中调整图像大小

标签 ios objective-c uiimage

我有这段代码可以在我的 UIImage 中获取图像(与名为“c1_1”的 IB 连接)并且应该保存在我的目录文件夹中:

IBOutlet UIImageView *c1_1;

if (c1_1.image != nil)
        {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/ImagensApp/ImagensClientes/%@_%@_Cliente.jpg",list[0],abc]];
            NSData* data = UIImageJPEGRepresentation(c1_1.image, 70);
            [data writeToFile:path atomically:YES];
        }

此代码有效,但我想将图像大小调整为 200x200,在保存之前如何执行此操作?

最佳答案

缩放图像只是几行代码

  • 创建内存位图
  • 将图像绘制到该位图中
  • 然后从位图中获取新图像

像这样

- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake( 0, 0, newSize.width, newSize.height )];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return( result );
}

关于ios - 在保存到目录之前在 UIImage 中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24470988/

相关文章:

iphone - 将 JSON 转换为 NSDictionary 或 NSArray - iOS

objective-c - 从 iOS 库中选择音乐并发送/保存

iphone - 在应用程序包中包含核心数据

ios - 为什么在我使用 reloadData 时 indexPathsForSelectedRows 会失败,我该如何解决?

ios - 以正确的方向保存图像 - Swift & Core Image

ios - 从 AVCaptureStillImageOutput 解压缩的 UIImage

iOS 用户界面测试 : Get image filename (Swift)

iphone - 使用NSCoding缓存文件的框架

ios - 您仍然可以使用 Xcode 10.1 在 App Store 上发布用 Swift 3 编写的应用程序吗?

ios - navigationItem.titleView 更新时闪烁