-2

わかりましたので、ユーザーにiPhoneの写真から画像を選択させ、その画像をボタンの画像として保存しようとしています。ユーザーがアプリをシャットダウンして再度開くと、ユーザーに保存した画像をもう一度表示してもらいたい:) theButtonには、クリックされたボタンの送信者が含まれています。

ユーザーが画像をボタン画像として配置できる部分を行うことができます。この行を使用して画像を設定します

[theButton setImage:btnImage forState:UIControlStateNormal];

ボタンの画像にどのようにアクセスできますか?それで、保存してロードできますか?

私はチュートリアルに従ってこのコードを使用しますが、読み取り専用であるため Imageview を使用する必要がないように見えるため、ロード部分で立ち往生しています:)そのための私のコード:

//SAVE
-(NSString *)pathOfFile{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsFolder = [paths objectAtIndex:0];
    return [documentsFolder stringByAppendingFormat:@"myfile.plist"];
}

-(void)applicationWillTerminate:(NSNotification*)notification{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    [array addObject: tileOne.imageView];
    [array addObject: tileTwo.imageView];
    [array writeToFile:[self pathOfFile] atomically:YES];
}

//LOAD
- (void)viewDidLoad
{
    NSString *filePath = [self pathOfFile];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
        //Here it says read only, what to use then?
        tileOne.imageView = [array objectAtIndex:0];

    }

    UIApplication *app = [UIApplication sharedApplication];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];

    [super viewDidLoad];

}
4

2 に答える 2

1

tileOne とはどのようなオブジェクトですか? それがUIButtonの場合、あなたはあなた自身の質問に答えました.

[tileOne setImage:[array objectAtIndex:0] forState:UIControlStateNormal];
于 2013-05-10T18:54:36.193 に答える
0

NSCodingを調べることもできます。UIImage *image;ヘッダーファイルにまたは何かがある場合、次のように使用できると思います

-(void)saveImage
{
    NSString *filePath =[documentsFolder stringByAppendingPathComponent:@"file.x";
    NSMutableData *dataToSave = [NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataToSave];
    [archiver encodeObject:image forKey:@"theImage"]; //if 'image' is I.E an UIImage
    [archiver finishEncoding];
    [dataToSave writeToFile filePath options:NSDataWritingAtomic error:&error];//do some additional error-stuff
}
-(void)openImage
{
    NSString *filePath =[documentsFolder stringByAppendingPathComponent:@"file.x";
    if([[NSFileManager defaultManager] fileExistsAtPath])
    {
        NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initWithReadingWithData:data];
        image = [unarchiver decodeObjectForKey:@"theImage"];
        [unarchiver finishDecoding];
    }
    else
    {
        //No images was previously saved, do something else, image = nil
    }
}
于 2013-05-11T14:13:15.713 に答える