cocoapodをmacにインストールする方法

cocoapodをインストールしたくなったので、調査
以下、リンクを参考に実施した使えそう。

【参考】
開発レシピ:Objective-Cのライブラリ管理ツール CocoaPods | iOS開発者@日本 - http://www.iosjp.com/dev/archives/451

ただし、rubyが古かったので、下記リンクを参考にバージョンあげてから、
インストール

【参考】
iPhone - CocoaPodsがインストール出来ない時 - Qiita - http://qiita.com/yimajo/items/e89749079d7d5f6cd481

[iOS] ライブラリ管理ツール「CocoaPods」の使い方 | CodeNote.net - http://codenote.net/ios/1918.html

以上、ハムかつを朝飯に食べ過ぎると太ることに気づいた堀でした。

iOSでOCRを使用(Tesseract)

OCRについて必要になったので、色々と調査

手順を抜粋すると以下内容で良いみたい
①tesseract-ocrのインポート
②tesseract-ios」のインポート※ラッパークラス
③プロジェクトの設定
C++ Language Dialect - Compiler Default
C++ Standard Library - Compiler Default
④言語ファイルのインポート
⑤実装

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
     
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
 
    // OCR実行
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 日本語を設定
        Tesseract *tesseract = [[Tesseract alloc] initWithDataPath:@"tessdata" language:@"jpn"];
         
        // OCRを実行する画像を設定
        [tesseract setImage:self.imageView.image];
         
        // OCR実行!
        [tesseract recognize];
         
        // 実行結果をアラートビューで表示
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            [[[UIAlertView alloc] initWithTitle:@"Tesseract Sample"
                                        message:[NSString stringWithFormat:@"Recognized:\n%@", [tesseract recognizedText]]
                                       delegate:nil
                              cancelButtonTitle:nil
                              otherButtonTitles:@"OK", nil] show];
        });
    });
}

【参考】
Tesseract ocr - http://www.slideshare.net/takmin/tesseract-ocr
SlideShareがまとめられて、わかりやすい。

OCRのライブラリに関して調査をしてみたのでまとめ - NAVER まとめ - http://matome.naver.jp/odai/2136642604546660501

gali8/Tesseract-OCR-iOS · GitHub - https://github.com/gali8/Tesseract-OCR-iOS
サンプルソース

Downloads - tesseract-ocr - An OCR Engine that was developed at HP Labs between 1985 and 1995... and now at Google. - Google Project Hosting - http://code.google.com/p/tesseract-ocr/downloads/list
→ダウンロードリンク先

iOSで日本語OCR!サンプルアプリ構築編〜iOS SDK 6.1 + tesseract-ocr 3.02〜 | Developers.IO - http://dev.classmethod.jp/etc/ios-tesseract-ocr-2/
→サンプルで動かす方法

以上、花粉症にはなっていない堀でした。

HTTP からファイルをダウンロードして、ローカルに保存する方法

// HTTP からファイルをダウンロードして、ローカルに保存、ローカルにファイルが存在すれば、ローカルのファイルをロード
//====================================================================================

- (void)loadImageFromRemote
{
    // 読み込むファイルの URL を作成
//    NSURL *url = [NSURL URLWithString:@"http://snippets.feb19.jp/wp-content/uploads/2013/01/Icon@2x.png"];
    NSURL *url = [NSURL URLWithString:@"http://ringflow.jp/wp-content/themes/u-design/assets/product1/ipod-1.jpg"];
    
    // 別のスレッドでファイル読み込みをキューに加える
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self
                                        selector:@selector(loadImage:)
                                        object:url];
    [queue addOperation:operation];
}
// 別スレッドでファイルを読み込む
- (void)loadImage:(NSURL *)url
{
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:url];
    
    // 読み込んだらメインスレッドのメソッドを実行
    [self performSelectorOnMainThread:@selector(saveImage:) withObject:imageData waitUntilDone:NO];
}
// ローカルにデータを保存
- (void)saveImage:(NSData *)data
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"ipod-1.jpg"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL success = [fileManager fileExistsAtPath:dataPath];
    if (success) {
        data = [NSData dataWithContentsOfFile:dataPath];
    } else {
        [data writeToFile:dataPath atomically:YES];
    }
    
    UIImage *image =  [[UIImage alloc] initWithData:data];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];
}
- (IBAction)startDownLoad:(id)sender {

    // HTTP からファイルをダウンロードして、ローカルに保存、ローカルにファイルが存在すれば、ローカルのファイルをロード
    //実行処理
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"ipod-1.jpg"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager fileExistsAtPath:dataPath];
    if (success) {
        NSLog(@"load from local");
        NSData *data = [NSData dataWithContentsOfFile:dataPath];
        
        UIImage *image =  [[UIImage alloc] initWithData:data];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [self.view addSubview:imageView];
    } else {
        NSLog(@"load from remote");
        [self loadImageFromRemote];
    }

}

【参考】
Objective-C (iOS): ウェブの画像をダウンロードして表示 | snippets.feb19.jp - http://snippets.feb19.jp/?p=410

以上、くまもんのグッズを爪切りを含めて色々持っている堀でした。

非同期での画像ダウンロード(iOS5以降)方法

iOS5から非同期処理が簡単になった為、以下のとおりのロジックでOK!

NSURL *URL = [NSURL URLWithString:_url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:NSURLResponse *response, NSData *data, NSError *error {
if (error) {
NSLog(@"error: %@", [error localizedDescription]);
return;
}

imageView.image = [UIImage imageWithData:data];
}];

※今までのダウンロード速度より1/2倍となっている。

【参考】
ios5 非同期で画像取得|アプリ開発:メモブログ - http://ameblo.jp/ucuz/entry-11422438440.html

[XCODE] iOSから非同期/同期にHTTPリクエストを送信する方法(Part2) - YoheiM .NET - http://www.yoheim.net/blog.php?q=20130206
※受信後の処理を処理単位に分岐するようにロジックがくまれているパターン

以上、蒸気でアイマスクを使用すると熟睡のレベルが違うなと思う堀でした。(ステマではない。)

非同期で画像をダウンロードして、表示

非同期について別で調査

SDWebImageを使用して、非同期で画像をダウンロードして、表示する方法

【やること】

1. ImageIO.framework を追加する
2. MapKit.framework を追加する

使用する「.m」ファイルの「.h」ファイルにインポート
#import "UIImageView+WebCache.h"

「.m」ファイルに以下を記載

        //UIImageに画像を設定
        //SDWebImageを使用して、非同期で画像を表示
        UIImageView *thumImageView = (UIImageView *)[cell viewWithTag:4];
        NSURL *imageURL = [NSURL URLWithString:[dic valueForKey:@"jsonThumbnail"]];
        UIImage *placeholderImage = [UIImage imageNamed:@"article-bottom-bar-download-button-highlighted"];
        [thumImageView setImageWithURL:imageURL
                   placeholderImage:placeholderImage];

【ダウンロード】
rs/SDWebImage - https://github.com/rs/SDWebImage

【参考】
SDWebImageによる画像表示とキャッシュ – Cyber Passion for iOS - http://blogios.stack3.net/archives/288
※細かいキャッシュの編集について記載

SDWebImageを初めて使う際の注意点 | masaplabs - http://www.masaplabs.com/use-sdwebimage-notice/

以上、最近、花金の意味がわからなくなってきた堀でした。

UITableViewで非同期でYouTubeから動画のフィードを取得して一覧表示する

非同期についてお勉強。

JSONを非同期で取得して、UITableViewに表示する方法

【参考】
Objective-C - UITableViewで非同期でYouTubeから動画のフィードを取得して一覧表示する | Cumiki - http://cumiki.com/hacks/24

以上、心臓がある方から話しかけないでくれよと思う堀でした。

Wordpress 外部phpファイルから投稿する処理

WordPressについて調査

Wordpress関数を呼び出す為に
require_once( dirname( __FILE__ ) . '/wp-load.php' );を使用したあとに
外部投稿処理を記述

以下Src

<?php
//WordPress関数を使用する為に読み込み
require_once( dirname( __FILE__ ) . '/wp-load.php' );
$post_value = array(
    'post_author' => 1,// 投稿者のID。
    'post_title' => 'テストタイトル',// 投稿のタイトル。
    'post_content' => 'テスト本文', // 投稿の本文。
    'post_category' => array(1,5), // カテゴリーID(配列)。
    'tags_input' => array('タグ1','タグ2'), // タグの名前(配列)。
    'post_status' => 'publish' // 公開ステータス。 
);
 
wp_insert_post($post_value);
?>

【参考】
WordPressでwp_insert_post関数を利用した投稿方法。(カスタムフィールド、WP to Twitter対応版) | ale cole blog - http://blog.ale-cole.com/php/wordpress/69/

WordPress › フォーラム » wp_insert_post関数の使い方 - http://ja.forums.wordpress.org/topic/18102

ちなみにWordPressにおける読み込みの階層についてわかりやすい記事
WordPressの実行フローを視覚化してみる | Simple Colors - http://www.warna.info/archives/279/

以上、明日髪を切りいく堀でした。