[objective-c]ELCImagePickerControllerを使ってカメラロールの写真を複数選択

ELCImagePickerControllerを使ってカメラロールの写真を複数選択

1) LibraryをLinkさせる
・プロジェクトを選択
・TARGETSを選択
・Build Phasesをクリック
・Link Binary With Librariesをクリック
・「+」をクリック
・AssetsLibrary.frameworkを選択して「add」
スクリーンショット

2)ヘッダファイルでimport

#import "ELCImagePickerController.h"
#import "ELCAlbumPickerController.h"

3)実装

-(float)getFillRatio:(CGSize)sourceSize targetSize:(CGSize)targetSize{
    CGFloat widthRatio  = targetSize.width  / sourceSize.width;
    CGFloat heightRatio = targetSize.height / sourceSize.height;
    return (widthRatio > heightRatio) ? widthRatio : heightRatio;
}
-(UIImage*)getResizeImageFill:(UIImage*)img size:(CGSize)_size{
    CGFloat ratio=[self getFillRatio:img.size targetSize:_size];
    CGSize resizedSize = CGSizeMake(roundf(img.size.width*ratio), roundf(img.size.height*ratio));
    
    UIGraphicsBeginImageContext(resizedSize);
    [img drawInRect:CGRectMake(0, 0, resizedSize.width, resizedSize.height)];
    UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resizedImage;
}




-(void)startPhotoSelect{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        ELCAlbumPickerController *albumController = [[[ELCAlbumPickerController alloc] initWithNibName:@"ELCAlbumPickerController" bundle:[NSBundle mainBundle]]autorelease];
        ELCImagePickerController *elcPicker = [[[ELCImagePickerController alloc] initWithRootViewController:albumController]autorelease];
        [albumController setParent:elcPicker];
        [elcPicker setDelegate:self];
        [self presentModalViewController:elcPicker animated:YES];
    } else {
    }
}
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
	[self dismissModalViewControllerAnimated:YES];
    if ([info count]<1) {
        return;
    }
    int i;
    NSMutableDictionary*d;
    CGSize imageSize=CGSizeMake(100, 100);
    for (i=0; i<[info count]; i++) {
        d=[info objectAtIndex:i];
        UIImageView *imageview;
        imageview=[[UIImageView alloc] initWithImage:[self getResizeImageFill:[d objectForKey:UIImagePickerControllerOriginalImage] size:imageSize]];
        [self.view addSubview:imageview];
        imageview.frame=CGRectMake(20.0*i, 20.0*i, imageSize.width, imageSize.height);
    }
}
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker {
	[self dismissModalViewControllerAnimated:YES];
}

//ここから実行
-(IBAction)whenTapBtn:(id)sender{
    [self startPhotoSelect];
}

github:ELCImagePickerController
https://github.com/elc/ELCImagePickerController

[objective-c]GCDを使った非同期処理

GCDを使った非同期処理
GCD:Grand Central Dispatch

dispatch_queue_t main=dispatch_get_main_queue();
dispatch_queue_t sub=dispatch_queue_create("net.ktyr.sample", NULL);
dispatch_async(sub, ^{
    //何か重たい処理
    UIImage*image=[UIImage imageNamed:@"thankyou.png"];
    dispatch_async(main, ^{
        //重たい処理が終わったとき
        [myImageView setImage:image];
    });
});
dispatch_release(sub);

[iPhone Apps]PhotoTile

iPhoneアプリ「フォトタイル」をリリースしました。

フォトタイルは複数の写真をササッとつなげて一枚にするアプリです。
写真を複数枚アップロードしたい!そんなときこれでササッとつないで投稿できます。
Twitter,Facebookに対応しており、作った写真をすぐにアップロードすることができます。
つなぎ方、角円などを調整して自分好みの設定で写真をつなぎましょう。

主な機能
■複数の写真をつなぐことができます。
■つなぎ方を選択できます。
・縦一列
・横一列
・格子状
・サイズ位置ランダム配置
■つないだ後に写真の順番を入れ替えられます。
■つないだ後に不要な写真を削除できます。
■縦横サイズを調整できます。
■縁の太さを調整できます。
■角円を調整できます。
■背景色を調整できます。
■写真をTwitterへアップロードできます。
■写真をFacebookへアップロードできます。
■写真を使用中のiPhone,iPodへ保存できます。

PhotoTile is an app that can combine multiple photos into single photo.
when you want to upload multiple photos, You can do it quickly with this app.
You can upload photos to Twitter,Facebook.

Features
■Enable to combine multiple photos.
■Enable to select combine method.
・vertical
・horizontal
・grid
・random position, random size
■Enable to change the order of the photos.
■Enable to delete unwanted photos.
■Enable to adjust vertical,horizontal size.
■Enable to adjust border thickness.
■Enable to adjust corner radius.
■Enable to adjust background color.
■Enable to upload photo to Twitter.
■Enable to upload photo to Facebook.
■Enable to save photo to your device.

フォトタイル
フォトタイル

PhotoTile(English)

フォトタイルフォトタイル

フォトタイルフォトタイル

フォトタイル

[objective-c]RGBAで指定した色のUIImageを生成

RGBAで指定した色のUIImageを生成

-(UIImage*)get255ColorRectImage:(float)r g:(float)g b:(float)b alpha:(float)alpha size:(CGSize)_size{
    UIGraphicsBeginImageContext(_size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, r/255.0,g/255.0,b/255.0,alpha/255.0);
    CGContextFillRect(context, CGRectMake(0.0,0.0,_size.width,_size.height));
    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenImage;
}

UIImage*redImage=[self get255ColorRectImage:255.0 g:0.0 b:0.0 alpha:255.0 size:CGSizeMake(100, 100)];

[objective-c]UITabBarの背景を削除する

UITabBarの背景を削除する

-(void)removeTabBarBackground:(UITabBar*)_tab{
    for(UIView*t in _tab.subviews){
        NSString*s=NSStringFromClass([t class]);
        if ([s isEqualToString:@"UITabBarButton"] || t==_tab) {
            continue;
        }
        [t removeFromSuperview];
        break;
    }
}

[self removeTabBarBackground:_mytab];

[objective-c]UITouchからCGPoint取得

UITouchからCGPoint取得

-(CGPoint)getCGPointByUITouch:(UITouch*)t{
    return [t locationInView:self.view.superview];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSArray*t=[touches allObjects];
    CGPoint tp1=[self getCGPointByUITouch:[t objectAtIndex:0]];
}

[objective-c]NSDateからNSString生成

NSDateからNSString生成

-(NSString*)getStringByDate:(NSDate*)_date{
    NSDateFormatter*df=[[NSDateFormatter alloc] init];
    NSCalendar*cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [df setCalendar:cal];
    [df setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSString* str=[df stringFromDate:_date];
    [cal release];
    [df release];
    return str;
}

[objective-c]GameCenter

なんとなく作ったGameCenterManager

#import "GameCenterManager.h"

GameCenterManager*gcm;
gcm=[[GameCenterManager alloc]initWithDelegate:self func:@selector(whenGameCenterEvent:)];
[gcm authenticateLocalPlayer];

-(void)whenGameCenterEvent:(NSNumber*)nm{
    NSLog(@"whenGameCenterEvent %d",[nm intValue]);
}

GameCenterManager.h

#import 
#import 

@interface GameCenterManager : NSObject{
    BOOL login;
@private
    
    id del;
    SEL func;
    /*
     1:認証成功
     2:認証失敗
     3:スコア報告成功
     4:スコア報告失敗
     5:リーダーボード表示失敗
    */
}
@property(nonatomic,assign)BOOL login;
-(id)initWithDelegate:(id)_d func:(SEL)_f;
- (void) authenticateLocalPlayer;
- (void) reportScore: (int64_t) score forCategory: (NSString*) category;
-(void)reportScore:(int64_t)score;
- (void) showLeaderboard;

@end

GameCenterManager.m


#import "GameCenterManager.h"

@implementation GameCenterManager

static NSString*leaderBoardID=@"hoge";

@synthesize login;

-(id)init{
    self=[super init];
    if (self) {
        login=NO;
    }
    return self;
}
-(id)initWithDelegate:(id)_d func:(SEL)_f{
    self=[self init];
    del=_d;
    func=_f;
    return self;
}
-(void)exeEvent:(int)t{
    NSNumber*n=[NSNumber numberWithInt:t];
    if (del && func && [del respondsToSelector:func]) {
        [del performSelector:func withObject:n];
    }
}



-(void)dealloc{
    del=nil;
    func=nil;
    [super dealloc];
}


//step1:プレーヤー認証 最速で実行
- (void) authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer authenticateWithCompletionHandler:^(NSError *error) {
        if (localPlayer.isAuthenticated){
            // 認証済みプレーヤーの追加タスクを実行する
            login=YES;
            [self exeEvent:1];
        }else{
            [self exeEvent:2];
        }
        }];
}


//step2:スコア報告
//category:リーダーボードのID
- (void) reportScore: (int64_t) score forCategory: (NSString*) category
{
    if (!login) {
        [self exeEvent:4];
        return;
    }
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category]autorelease];
    scoreReporter.value = score;
    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
        if (error != nil){
            // 報告エラーの処理
            [self exeEvent:4];
        }else{
            [self exeEvent:3];
        }
        }];
}
-(void)reportScore:(int64_t)score{
    [self reportScore:score forCategory:leaderBoardID];
}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController*)viewController
{
    [viewController dismissModalViewControllerAnimated:YES];
}

//リーダーボード表示
- (void) showLeaderboard
{
    GKLeaderboardViewController *leaderboardController =[[[GKLeaderboardViewController alloc] init]autorelease];
    if (leaderboardController != nil)
    {
        leaderboardController.leaderboardDelegate=self;
        [del presentModalViewController: leaderboardController animated: YES];
    }else{
        [self exeEvent:5];
    }
}

@end