首页 文章

UIImage返回nil

提问于
浏览
0

我将创建一个 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 回答

  • 0

    我认为你甚至在设置它之前就试图使用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];
    
    }
    
  • 0

    第一次将 imgView.image 设置为值时,接近 imagePickerController:didFinishPickingMediaWithInfo: 的末尾 . 因此,第一次调用此方法时, imgView.image 将成为 nil ,直到最后设置为:

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

    对不起,要跳过阅读你的代码,发布对我有用的内容 .

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

相关问题