Aqlier/ 6月 29, 2016/ iPhone

Xcode 開発で、TableViewCamera Rollの写真を表示する方法

TableViewで、行にあたるものは、”Cell”と呼び、画像とテキストの表示が可能です。

この画像にあたるものを、固定でなく、明細に該当したiphone内のCamera Rollの画像を表示する方法をご紹介します。

iphone上のCamera Roll の状態
スクリーンショット 2016-06-29 16.35.44
スクリーンショット 2016-06-29 16.35.56

アプリ内で、画像とテキスト情報を紐づける処理を行っていますが、ここでは省略します。

◆まず、TableViewに文字を表示する方法

 ToDoなどのアプリで使用されている方法になります。
 今回、Cell上に画像、テキストを表示するポイントを中心にした内容になります。

storyboard上の定義
スクリーンショット 2016-06-29 15.54.07

mファイル上の定義
表示するデータを一旦配列に入れて、「日付 + コメント」に編集して表示する方法になっています。


//Cell上の定義
@property (weak, nonatomic) IBOutlet UITableViewCell *textLabel;

===========================================================================
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

~~~
NSString *Wdate = [item sdate]; //指定日
NSString *Wcome = [item come]; //コメント

NSString *str50 = [Wdate stringByAppendingString:@" "];//空白を付加
NSString *str53 = [str50 stringByAppendingString:Wcome];

cell.textLabel.text = str53; //分類、指定日、コメントを表示

~~~
}


この場合の実行結果(日付 + コメントで表示されます。

スクリーンショット 2016-06-29 15.40.06

◆ここから、TableViewにcamera Rollの写真を表示する方法です。

 

mファイル上の定義
TableViewに画像とテキストを表示する方法で、画像を表示する部分の実装部分です。
表示するデータを一旦配列に入れて配列から画像のURLを取得して、URL型に変換して実際の画像にアクセスします。


//Cell上の定義
@property (weak, nonatomic) IBOutlet UITableViewCell *imageView;//画像
@property (weak, nonatomic) IBOutlet UITableViewCell *textLabel;//テキスト
===========================================================================

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

~~~
 CGFloat width = 40; // リサイズ後幅のサイズ
 CGFloat height = 40; // リサイズ後高さのサイズ
//画像表示
NSString *Wgazoou = [item gazou];

NSURL* aURL = [NSURL URLWithString:Wgazoou];
 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
 
 [library assetForURL:aURL resultBlock:^(ALAsset *asset)
 
 { UIImage *copyOfOriginalImage =
 [UIImage imageWithCGImage:[[asset defaultRepresentation]
 fullScreenImage]
 scale:1.0 orientation:UIImageOrientationUp]; //数値が小さいと 大きい画像になる。
 
 UIGraphicsBeginImageContext(CGSizeMake(width, height));
 
 [copyOfOriginalImage drawInRect:CGRectMake(0, 0, width, height)];
 
 UIGraphicsEndImageContext();

cell.imageView.image = copyOfOriginalImage;
 cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
 }
 failureBlock:^(NSError *error) { }];

~~~
}

この場合の実行結果は、以下のようになります。
画像の表示が次画面経由でしか表示されないため、NGです。
左:画面起動時                右:次画面から戻った時
スクリーンショット 2016-06-29 16.20.13
スクリーンショット 2016-06-29 16.20.25

cellのbackgroundに画像を入れて表示させる方法で画像を画面表示させることもできますが、cellが選択されるとbackgroundの画像が、左端の大きさから、巨大化(Cellの大きさ)で表示され、Apple審査でNGになりました。この方法では、審査を通してもらえません。

◆上記の問題を解決するために行った方法

ダミーの画像を一旦表示することで対処しました。

@property (weak, nonatomic) IBOutlet UITableViewCell *imageView;
@property (weak, nonatomic) IBOutlet UITableViewCell *textLabel;

===========================================================================

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

~~~
UIImage *img_mae; // リサイズ後UIImage
UIImage *img_ato; // リサイズ後UIImage

CGFloat width = 40; // リサイズ後幅のサイズ
CGFloat height = 40; // リサイズ後高さのサイズ

img_mae = [UIImage imageNamed:@"Icon-40@2x.png"]; //表示前にDSPロゴを表示。

UIGraphicsBeginImageContext(CGSizeMake(width, height));

[img_mae drawInRect:CGRectMake(0, 0, width, height)];

img_ato = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

cell.imageView.image = img_ato;

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;


//画像表示

NSString *Wgazoou = [item gazou];

NSURL* aURL = [NSURL URLWithString:Wgazoou];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library assetForURL:aURL resultBlock:^(ALAsset *asset)

{ UIImage *copyOfOriginalImage =
[UIImage imageWithCGImage:[[asset defaultRepresentation]
fullScreenImage]
scale:1.0 orientation:UIImageOrientationUp]; //数値が小さいと 大きい画像になる。

UIGraphicsBeginImageContext(CGSizeMake(width, height));

[copyOfOriginalImage drawInRect:CGRectMake(0, 0, width, height)];

// img_ato = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

cell.imageView.image = copyOfOriginalImage;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
}
failureBlock:^(NSError *error) { }];

~~~
}

この場合の実行結果
ダミーの画像を一瞬表示させることで、この問題は解決できました。
表示タイミングの問題と言えますが、ロジック上でダミー処理追加で解決したため、根本の原因は不明です。


スクリーンショット 2016-06-29 15.35.37