1

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

然后我尝试调整图像大小,保存它,并将保存的图像文件检索到UIImageView. 但由于某种原因,图像从未显示 theUIImageView和 lastNSLog返回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"]);

}
4

1 回答 1

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

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

[UIImage imageWithContentsOfFile:fullPathToFile];
于 2013-05-12T13:56:22.003 回答