3D技術研究所Wiki > TGAReader > TGA画像をiOSアプリケーションで利用する

TGA画像をiOSアプリケーションで利用する方法についてまとめています。

目次


TGA画像をiOSアプリケーションで利用する

TGAReaderを利用してUIImageを作成します。

TGAImageViewer_iOS.png

TGA画像をiOSアプリケーションで利用するソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#import "TGACollectionViewController.h"
#include "tga_reader.h"

@interface TGACollectionViewController () {
    NSArray *images;
}
@end

@implementation TGACollectionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    images = [NSArray arrayWithObjects:
              @"grayscale_LL",
              @"grayscale_rle_LL",
              @"grayscale_a_LL",
              @"grayscale_a_rle_LL",
              @"indexed_LL",
              @"indexed_rle_LL",
              @"indexed_a_LL",
              @"indexed_a_rle_LL",
              @"rgb_LL",
              @"rgb_rle_LL",
              @"rgb_a_LL",
              @"rgb_a_rle_LL",
              nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
    NSString *path = [[NSBundle mainBundle] pathForResource:[images objectAtIndex:indexPath.row] ofType:@"tga"];
    imageView.image = [self createTGAImage:path];
    
    return cell;
    
}

- (UIImage *)createTGAImage:(NSString *)path {
    
    FILE *file = fopen([path UTF8String], "rb");
    if(file) {
        fseek(file, 0, SEEK_END);
        int size = ftell(file);
        fseek(file, 0, SEEK_SET);
        
        unsigned char *buffer = (unsigned char *)tgaMalloc(size);
        fread(buffer, 1, size, file);
        fclose(file);
        
        int width = tgaGetWidth(buffer);
        int height = tgaGetHeight(buffer);
        int *pixels = tgaRead(buffer, TGA_READER_ABGR);
        
        tgaFree(buffer);
        
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = (CGBitmapInfo)kCGImageAlphaLast;
        CGDataProviderRef providerRef = CGDataProviderCreateWithData(NULL, pixels, 4*width*height, releaseDataCallback);
        
        CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4*width, colorSpaceRef, bitmapInfo, providerRef, NULL, 0, kCGRenderingIntentDefault);
        
        UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
        
        CGColorSpaceRelease(colorSpaceRef);
        
        return image;
    }
    
    return nil;
    
}

static void releaseDataCallback(void *info, const void *data, size_t size) {
    tgaFree((void *)data);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


関連ページ


スポンサードリンク


添付ファイル: fileTGAImageViewer_iOS.png 274件 [詳細]
トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS