2012年2月25日土曜日

[objective-c] iPadでのUIImagePickerControllerの使い方

カメラを使う時のクラスとしてUIImagePickerControllerがありますが
iPadでこれを使う場合、iPhoneアプリと同様にコードを書くと動かない場合があります。


UIImagePickerController imagePicker = [[UIImagePickerController alloc] init];
 imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
 imagePicker.mediaTypes = [NSArray arrayWithObject:@"public.image"];
 imagePicker.delegate = self;
 [self presentModalViewController:imagePicker animated:YES];
 [imagePicker release];

iPhoneアプリ同様でOK


UIImagePickerController imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
 [self presentModalViewController:imagePicker animated:YES];
 [imagePicker release];

としたいところですがこれでは駄目

UIImagePickerController imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
UIPopoverController uiPopoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[uiPopoverController presentPopoverFromBarButtonItem:targetElm // 基点となるUI
                                       permittedArrowDirections:UIPopoverArrowDirectionAny 
                                                       animated:YES];

UIPopoverControllerでラップすることによって表示することが可能です。

2012年2月23日木曜日

[ツール] syntaxhighliter 3.xをobjective-cに対応させる

ブログにソースを乗っける時の定番のsyntaxhighlighterですが
標準ではobjective-cに対応していません。

調べたところ有志の方がobjective-c用のプラグインを作成していますので
http://blog-imgs-47.fc2.com/d/e/v/dev2next/shBrushObjectiveC.js
↑からjsファイルをダウンロードし、headタグ内にincludeします。
(参照:アプリ開発の記録

利用する箇所で
<pre class="brush: objc";></pre>とソースコードを囲めば、Xcodeでコードしたような表示が可能です。


・おまけ
syntaxhighlighterが未導入の方は以下のサイトからincludeタグを生成すると楽です。

HOW TO ADD SYNTAX HIGHLIGHTER(V3) TO BLOGGER BLOGS

リンク切れが心配な方は全部のjsファイルを引っ張っておきませう

2012年2月22日水曜日

[objective-c] objective-cでAsyncTaskみたいな非同期処理

testOperation.h
@interface testOperation : NSOperation {
    BOOL isExecuting, isFinished;
}
@end

testOperation.m
#import "testOperation.h"

@implementation testOperation

-(void) dealloc {
    [super dealloc];
}
- (BOOL)isExecuting {
    return isExecuting;
}
- (BOOL)isFinished {
    return isFinished;
}

/**
 * 処理メイン
 */
-(void) main {
    LOG_METHOD;
    for (;;) {
        // 〜適当なループ処理など
        if ([self isCancelled])
                break; // キャンセルの場合にループを抜けるなりreturnするなり
    }
}

@end


呼び出し元
- (void) hogehoge {
    NSOperationQueue * _queue;
    _queue = [[NSOperationQueue alloc] init];
    // 1つ目のリクエスト
    testOperation *ope1 = [[[testOperation alloc] init]retain];;
    [ope1 autorelease];
    // 終了監視
    [ope1 addObserver:self forKeyPath:@"isFinished" 
                   options:NSKeyValueObservingOptionNew context:nil];
    // キュー実行
    [_queue addOperation:ope1];
}

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object 
                        change:(NSDictionary*)change context:(void*)context
{
    LOG_METHOD;
    // タスク終了時の処理
    // 〜〜〜〜

    // キー値監視を解除する
    [object removeObserver:self forKeyPath:keyPath];
}

[音楽] 心を開いて / ZARD



今聞いてもいい曲。