从UIImagePickerController调整图像大小不起作用

时间:2013-05-12 13:52:57

标签: ios cocoa-touch uiimagepickercontroller

我正在关注如何调整图片大小的this帖子。我将+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;放入selectExerciseImageViewController.h并将相关代码复制到selectExerciseImageViewController.m

然后我尝试调整图像大小,保存图像并将保存的图像文件检索到UIImageView。但由于某种原因,图片永远不会显示UIImageView,而最后NSLog则会显示null

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissModalViewControllerAnimated:YES];

  UIImage* newImage = [selectExerciseImageViewController imageWithImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"]
      scaledToSizeWithSameAspectRatio:CGSizeMake(40.0,40.0)];

    NSData* imageData = UIImagePNGRepresentation(newImage);
    // Give a name to the file
    NSString* imageName = @"MyImage.png";

    // Now, we have to find the documents directory so we can save it
    // Note that you might want to save it elsewhere, like the cache directory,
    // or something similar.
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
    //imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    _testimg.image = [UIImage imageNamed:@"MyImage.png"];
    NSLog(@"asd %@",[UIImage imageNamed:@"MyImage.png"]);

}

1 个答案:

答案 0 :(得分:2)

[UIImage imageNamed:@"MyImage.png"];

这会从主应用程序包中加载图像。您正在将文件写入文档目录。您需要使用不同的方法实例化UIImage对象。尝试:

[UIImage imageWithContentsOfFile:fullPathToFile];