首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >-initWithContentsOfFile:对于NSMutableArray

-initWithContentsOfFile:对于NSMutableArray
EN

Stack Overflow用户
提问于 2012-04-06 07:39:58
回答 2查看 800关注 0票数 0

我将图片存储在NSMutableArray中,现在我正在尝试让它们显示在viewDidLoad中。我试着给initWithContentsOfFile打电话,但似乎不起作用。代码是这样的:imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];

我不确定我应该使用什么来代替initWithContentsOfFile来加载保存的图像,我甚至不确定我是否可以通过用户默认设置将图像保存在plist中。我已经研究了一段时间了,但一无所获。任何帮助都非常感谢,谢谢!

编辑:以下是附加代码:

代码语言:javascript
复制
- (IBAction)grabImage {
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        _popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
        [_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    } 

    else {
        [self presentModalViewController:imgPicker animated:YES];
    }
    [self.imgPicker resignFirstResponder];
}
// Sets the image in the UIImageView
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    if (imageView.image == nil) {
        imageView.image = img;

        [self.array addObject:imageView.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;

    }

    if (imageView2.image == nil) {
        imageView2.image = img;
        NSLog(@"The image is a %@", imageView);
        [self.array addObject:imageView2.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }

    if (imageView3.image == nil) {
        imageView3.image = img;

        [self.array addObject:imageView3.image];

        [picker dismissModalViewControllerAnimated:YES];
        [self.popover dismissPopoverAnimated:YES];
        return;
    }
}

- (void)applicationDidEnterBackground:(UIApplication*)application {
    NSLog(@"Image on didenterbackground: %@", imageView);

   [self.array addObject:imageView.image];
    [self.array addObject:imageView2.image];
     [self.array addObject:imageView3.image];


            [self.user setObject:self.array forKey:@"images"];
    [user synchronize];

            }

- (void)viewDidLoad
    {
        self.user = [NSUserDefaults standardUserDefaults];
        NSLog(@"It is %@", self.user);
        self.array = [[self.user objectForKey:@"images"]mutableCopy];
        imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];
        imageView2.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:1]];
        imageView3.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:2]];




        UIApplication *app = [UIApplication sharedApplication];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidEnterBackground:)
                                                     name:UIApplicationDidEnterBackgroundNotification
                                                   object:app];

        backToGalleryButton.hidden = YES;
        tapToDeleteLabel.hidden = YES;
        deleteButton1.hidden = YES;
        [super viewDidLoad];

    }

编辑:这就是我如何标记图像,并根据它们的标记删除它们:

代码语言:javascript
复制
- (IBAction)deleteButtonPressed:(id)sender {
    NSLog(@"Sender is %@", sender);
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
    [deleteAlertView show];

    int imageIndex = ((UIButton *)sender).tag;
    deleteAlertView.tag = imageIndex;
}

- (UIImageView *)viewForTag:(NSInteger)tag {
    UIImageView *found = nil;
    for (UIImageView *view in self.array) {
        if (tag == view.tag) {
            found = view;
            break;
        }
    }
    return found;
}

- (void)alertView: (UIAlertView *) alertView 
clickedButtonAtIndex: (NSInteger) buttonIndex
{


    if (buttonIndex != [alertView cancelButtonIndex]) {
        NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);
        NSLog(@"The tag is %i", alertView.tag);

        UIImageView *view = [self viewForTag:alertView.tag];
        if (view) {
            [self.array removeObject:view];
        }

        NSLog(@"After deleting item, array count  = %d", [array count]);
    NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view);
        ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
    }

    [self.user setObject:self.array forKey:@"images"];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-06 08:37:14

问题是,您不能将图像存储在属性列表中,而当您将其保存在用户默认设置中时,您就会尝试这样做。您需要使用归档程序将图像转换为可以存储的NSData对象。

票数 2
EN

Stack Overflow用户

发布于 2012-04-06 08:03:11

看起来您没有将有效的图像路径传递给初始化器方法。确保路径正确,并且包含图像扩展名。

不过,在这种情况下,你真的不应该调用initWithContentsOfFile:,因为UIImageViewimage属性在你设置图像时会保留图像。这通常会导致内存泄漏(除非你正在使用自动引用计数)。请改用一个静态初始化器,比如imageNamed:,它的额外好处是使用系统缓存,并根据设备的特征自动加载正确版本的图像(例如,如果设备有视网膜显示器,它将加载图像的更高分辨率变体)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10037502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档