0

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

请注意,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;
}
4

1 回答 1

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];
于 2012-06-27T09:24:55.290 回答