UIImage返回零
我将创build一个UIImage
但如果我debugging该方法返回nil
。 我希望有人知道我必须做什么。
我的代码:
返回nil:
UIImage *image1 = imgView.image;
整个代码块:
UIImage *image1 = imgView.image; NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"]; [UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
更新:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { toolbar.hidden = NO; UIImage *image1 = imgView.image; NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"]; [UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES]; } if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) { toolbar.hidden = NO; UIImage *image1 = imgView.image; NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"]; [UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES]; } if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) { toolbar.hidden = NO; UIImage *image1 = imgView.image; NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"]; [UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES]; } else{ toolbar.hidden = NO; } imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; [self dismissViewControllerAnimated:YES completion:nil]; }
我认为你正在尝试使用imgView.image属性,甚至在你设置它之前。
imageView.image正在设置此行
imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
将该行移到方法的开头。 喜欢这个
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { toolbar.hidden = NO; UIImage *image1 = imgView.image; NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"]; [UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES]; }
你第一次设置imgView.image
的值接近imagePickerController:didFinishPickingMediaWithInfo:
的结尾imagePickerController:didFinishPickingMediaWithInfo:
因此,第一次调用这个方法时, imgView.image
将会是nil
直到它被设置为:
imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
对不起,要跳过阅读你的代码,只是发布什么对我有用。
- (UIImage *) getImage{ NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:@"somefile.png" ofType:nil]; if ([[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName]) { return [UIImage imageNamed:@"somefile.png"]; } else{ NSString *t = [self pathToImageInCache:@"somefile.png"]; if ([[NSFileManager defaultManager] fileExistsAtPath:t]){ NSLog(@"fiel exists"); return [[UIImage alloc]initWithContentsOfFile:t]; } else{ NSLog(@"fiel does not exists"); } } return nil; } - (NSString *) pathToImageInCache: (NSString *) imageName{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@""]; NSString *URLImg = [documentsDirectory stringByAppendingPathComponent:imageName]; return URLImg; }