我将创建一个 UIImage
但如果我调试该方法它返回 nil
. 我希望有人知道我要做什么 .
My code:
返回nil:
UIImage *image1 = imgView.image;
Whole block of code:
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
Update:
- (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];
}
3 回答
我认为你甚至在设置它之前就试图使用imgView.image属性 .
正在使用此行设置imageView.image
将该行移动到方法的开头 . 像这样
第一次将
imgView.image
设置为值时,接近imagePickerController:didFinishPickingMediaWithInfo:
的末尾 . 因此,第一次调用此方法时,imgView.image
将成为nil
,直到最后设置为:对不起,要跳过阅读你的代码,发布对我有用的内容 .