我正在关注有关如何调整图像大小的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"]);
}
最佳答案
[UIImage imageNamed:@"MyImage.png"];
这将从主应用程序包中加载图像。您正在将文件写入documents目录。您需要使用其他方法实例化
UIImage
对象。尝试:[UIImage imageWithContentsOfFile:fullPathToFile];
关于ios - 从UIImagePickerController调整图像大小无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16508087/