在Objective C中存储重新调整大小的图像

时间:2012-06-27 07:37:06

标签: objective-c ios image-resizing

我正在编写一个应用程序,让用户可以选择调整他们拍摄的图像并将其上传到服务。调整大小似乎工作正常,但它不是将重新创建的图像存储在适当的位置,因为应用程序似乎并不认为有一张照片已加载。

请注意,if语句中的第一个函数(关于“normal_size”)工作正常。我试图在额外的大小参数中重新创建这两行,但我似乎无法弄清楚它为什么不存储它。这真让我抓狂。这是代码:

// Set image dictionaries
iInfoDict = [[NSDictionary alloc] initWithDictionary:info];
NSLog(@"infor ===== %@", info);
UIImage* image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSLog(@"iImage name === %@", iImage);

// Determines path of image taken / chosen
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"image.jpg"];

CGSize size;

// Not sure what this does other than dismissing the pickerViewController

NSData* data = UIImageJPEGRepresentation(image, 0.9);
[data writeToFile:appFile options:nil error:nil];
NSLog(@"Documents ==== %@", documentsDirectory);
[picker dismissModalViewControllerAnimated:YES];
//    photo.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

// IMAGE RESIZE CODE

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"normal_image"])    
{
    [photoData setObject:[iInfoDict objectForKey:@"UIImagePickerControllerOriginalImage"] forKey:@"image"];
     photo.image = [iInfoDict objectForKey:@"UIImagePickerControllerOriginalImage"];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"small_image"])
{
    CGSize size = CGSizeMake (600, 450);
    UIImage *origImage = [iInfoDict objectForKey:@"UIImagePickerControllerOriginalImage"];
    UIImage *destImage = [origImage resizedImage:size interpolationQuality:kCGInterpolationHigh];

    NSLog(@"Image Size: %@", NSStringFromCGSize(destImage.size));
    [photoData setObject:destImage forKey:@"image"];

    photo.image = destImage;
}

1 个答案:

答案 0 :(得分:0)

要么我不理解你的问题,要么你不明白你在做什么。我怀疑后者,基于注释// Not sure what this does other than dismissing the pickerViewController,后面紧跟两行,将未显示的图像写入文档目录。

您可能希望在写出之前调整图像的大小,所以我建议您在调整大小后将文字移到最后。

删除两行:

NSData* data = UIImageJPEGRepresentation(image, 0.9);
[data writeToFile:appFile options:nil error:nil];

最后,插入这两行(不保证,从内存中写入):

NSData* data = UIImageJPEGRepresentation(photo.image, 0.9);
[data writeToFile:appFile options:nil error:nil];