UIImage 返回 nil [英] UIImage returns nil

查看:73
本文介绍了UIImage 返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将创建一个 UIImage 但如果我调试该方法,它将返回 nil.我希望有人知道我必须做什么.

I'll be creating an UIImage but if I debug the method it returns nil. I hope someone know what I have to do.

我的代码:

返回零:

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 属性之前就尝试使用它.

I think you are trying to use the imgView.image property even before you set it.

imageView.image 正在用这一行设置

imageView.image is being set with this line

imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

将该行移动到方法的开头.像这样

Move that line to beginning of the method. like this

- (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];

}

这篇关于UIImage 返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆