在保存到目录之前,在UIImage内调整图像大小

时间:2014-06-28 21:09:49

标签: ios objective-c uiimage

我有这段代码可以将图片放入我的UIImage(与IB连接,名为' c1_1')并保存在我的目录文件夹中:

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,我可以在保存之前完成此操作吗?

1 个答案:

答案 0 :(得分:1)

缩放图像只需几行代码

  • 创建一个内存位图
  • 将图像绘制到该位图
  • 然后从位图
  • 获取新图像
像这样

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

    return( result );
}