2013-05-12 57 views
1

我正在關注如何調整圖像的大小this。我將+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;放入selectExerciseImageViewController.h,並將相關代碼複製到selectExerciseImageViewController.m調整圖像從UIImagePickerController不起作用

然後我嘗試調整圖像大小,保存圖像,並將保存的圖像文件檢索到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"]); 

} 

回答

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

這從主應用程序包加載圖像。您正在將文件寫入文檔目錄。您需要用不同的方法實例化對象UIImage。嘗試:

[UIImage imageWithContentsOfFile:fullPathToFile]; 
+0

有沒有辦法將文件保存到我的應用程序? – 2013-05-12 13:59:53

+0

不,您不能修改應用程序包,因爲它是數字簽名的,任何修改都會使您的應用程序無法啓動。 Apple提供了特定的目錄供您寫入,您應該使用這些。 – Jim 2013-05-12 14:00:57

+0

當我檢查我的相冊時,我應該看到我的文件的裁剪版本正確嗎?我如何獲得fullPathToFile? – 2013-05-12 14:05:13