Ios 从UIImagePickerController调整图像大小不工作
我下面是关于如何调整图像大小的帖子。我将Ios 从UIImagePickerController调整图像大小不工作,ios,cocoa-touch,uiimagepickercontroller,Ios,Cocoa Touch,Uiimagepickercontroller,我下面是关于如何调整图像大小的帖子。我将+(UIImage*)图像放置在image:(UIImage*)图像缩放的tosize:(CGSize)newSize中
+(UIImage*)图像放置在image:(UIImage*)图像缩放的tosize:(CGSize)newSize中在中选择exerciseimageviewcontroller.h
并将相关代码复制到选择exerciseimageviewcontroller.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
对象。尝试:
[UIImage imageWithContentsOfFile:fullPathToFile];
不,您不能修改应用程序捆绑包,因为这是数字签名的,任何修改都会使您的应用程序无法启动。苹果公司提供了特定的目录供您写入,您应该使用这些目录。当我检查我的相册时,我应该会看到我的文件的裁剪版本正确吗?。如何获取fullPathToFile?不,简单地将图像保存到磁盘不会自动将其添加到任何相册中。您需要为此使用正确的API。您已经有了fullPathToFile
——请查看您发布的代码。你是不是只是从某个地方复制粘贴了它,然后在没有阅读的情况下寻求帮助?不,我只是不知道它到底是如何工作的。因此,我感到困惑。谢谢:)
[UIImage imageWithContentsOfFile:fullPathToFile];