ユニットテストでUserのインスタンスを取得する
ローカルのユニットテストで、com.google.appengine.api.users.Userのインスタンスを取得しようとしても、デフォルト・コンストラクタはプライベートであるし、UserServiceのgetCurrentUser()メソッドもnullを返すため、簡単ではない。
以下のリンクを参考にすれば、getCurrentUser()メソッドで任意のuserIdを持つUserのインスタンスを取得することが可能。
Local Unit Testing with UserService: userId==null problem
また、slim3を利用する場合は、下記のようにtester.environmentからorg.slim3.tester.TestEnvironmentを利用するらしい。
public class UserTest extends AppEngineTestCase { @Before @Override public void setUp() throws Exception { super.setUp(); Map<String, Object> envAttributes = new HashMap<String, Object>(); envAttributes.put( "com.google.appengine.api.users.UserService.user_id_key", "42"); tester.environment.setEmail("admin@example.com"); tester.environment.setAttributes(envAttributes); } @Test public void instantiateUser() { UserService service = UserServiceFactory.getUserService(); User user = service.getCurrentUser(); assertThat(user, is(notNullValue())); assertThat(user.getUserId(), equalTo("42")); assertThat(user.getEmail(), equalTo("admin@example.com")); } }
NSDateを日付に変換する
はじめにNSDateComponentsに一度変換する必要がある。この変換にはNSCalendarを必要とする。
- (NSDateComponents *)componentsFromDate:(NSDate *)date withCalendar:(NSCalendar *)calendar { /* calendarが指定されていない場合には、現在のカレンダーを指定する */ if (!calendar) { calendar = [NSCalendar currentCalendar]; } /* 日付のコンポーネントとして、年月日時分秒を取得する */ static unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; return [calendar components:unitFlags fromDate:date]; }
NSDateComponentsを取得後は、下記のように日付を取得する。
NSDate *date = [NSDate date]; NSDateComponents *components = [self componentsFromDate:date withCalendar:nil]; NSLog(@"%d年 %d月 %d日 %d時 %d分 %d秒", [components year], [components month], [components day], [components hour], [components minute], [components second]);
Navigation-Based ApplicationでToolBarを表示する
ToolBarを表示しようとしているViewを所有しているControllerの
- -(void)viewDidLoad
- -(void)viewDidAppear:(BOOL)animated
- -(void)viewWillDisappear:(BOOL)animated
を下記のようにオーバーライドする。
- (void)viewDidLoad { [super viewDidLoad]; /* ToolBarの中心にカメラのアイコンを表示するようにToolBarを設定する */ UIBarButtonItem *leftSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]; UIBarButtonItem *cameraButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(cameraButtonPressed:)] autorelease]; UIBarButtonItem *rightSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]; [self setToolbarItems:[NSArray arrayWithObjects: leftSpace, cameraButton, rightSpace, nil] animated:NO]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; /* Viewが表示されたときに、ToolBarを表示する */ [[self navigationController] setToolbarHidden:NO animated:animated]; } - (void)viewWillDisappear:(BOOL)animated { /* Viewが隠されたときに、ToolBarを隠す */ [[self navigationController] setToolbarHidden:YES animated:animated]; [super viewWillDisappear:animated]; }
ImagesService#getServingUrl()からサムネールのURLを生成する
ImagesService#getServingUrl(BlobKey blobKey)を使用すると,Blobstoreに保存されている画像のURLを取得することができる.
さらに,取得したURLの末尾に「=s144」などと付け加えることで,144×144の正方形に収まる大きさの画像のURLを生成できる.(アスペクト比はオリジナル画像と同一.)
また,指定したサイズにトリミングする場合は,URLの末尾を「=s144-c」などとする.
サイズとして指定できる数値はImagesService.SERVING_SIZES,ImagesService.SERVING_CROP_SIZESに定義されている.
リサイズのロジックを含んだハンドラを,都度用意する必要がないためとても便利.