カメラロールといえば、皆さんご存知のように、
[アルバム]→[サムネイル]
の順で選択してくるあの画面です。
iPhoneではAndroidと違い、アプリ間連携がありませんので、
iPhoneアプリ内で実装しなければいけません。
難しいかなぁと思っていたのですが、
UIImagePickerControllerを使い簡単に出来たので、
ソースコードをGitHubにアップしました。
参考:morodomi / ImagePickerController
解説しますと、
#pragma mark -
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
// 成功の場合のブロックを作成
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
// サムネイル画像のReferenceを作成
CGImageRef thumbrefs = [myasset thumbnail];
// あれば、XIBで作ったImageViewへ入れる
if (thumbrefs) {
UIImage *thumbnail = [UIImage imageWithCGImage:thumbrefs];
[thumbView setImage:thumbnail];
}
// 拡大画像は下記方法で取得
ALAssetRepresentation *representation = [myasset defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]
scale:[representation scale]
orientation:[representation orientation]];
[imageView setImage:image];
};
// 失敗した場合の処理
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
// とくに、位置情報サービスがOFFになっている場合、
// 写真が取得出来無いので、位置情報サービスをONにして下さいという
// アラームを出した方が良い。今回は実装していない。
NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
};
if(imageURL) {
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:failureblock];
}
[self dismissModalViewControllerAnimated:YES];
}
というような感じで、取得後のメソッドを書いてしまえば、
あとはGitHubのソースコードと大きな違いが無く実装出来るとおもいます。

0 件のコメント:
コメントを投稿