【问题标题】:Resizing an image from UIImagePickerController not working从 UIImagePickerController 调整图像大小不起作用
【发布时间】:2013-05-12 13:52:57
【问题描述】:

我正在关注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"]);

}

【问题讨论】:

    标签: ios cocoa-touch uiimagepickercontroller


    【解决方案1】:
    [UIImage imageNamed:@"MyImage.png"];
    

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

    [UIImage imageWithContentsOfFile:fullPathToFile];
    

    【讨论】:

    • 不,您不能修改应用程序包,因为这是经过数字签名的,任何修改都会使您的应用程序无法启动。 Apple 提供了特定的目录供您写入,您应该使用这些目录。
    • 当我检查我的相册时,我应该看到我的文件的裁剪版本对吗?我如何获得 fullPathToFile??
    • 不,只是将图像保存到磁盘不会自动将其添加到任何相册。您需要为此使用正确的 API。
    • 您已经拥有fullPathToFile - 查看您发布的代码。您是否只是从某个地方复制并粘贴了此内容,然后甚至没有阅读就寻求帮助?
    • 不,我只是不知道这到底是如何工作的。因此,我很困惑。谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2014-08-29
    • 2014-05-12
    • 1970-01-01
    • 2013-04-04
    • 2014-04-28
    相关资源
    最近更新 更多