我正在关注这篇关于如何调整图像大小的帖子。我将相关代码放入并将+ (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"]);
}