태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.

mobile/ios2011/09/22 12:27

이번에 참여하는 프로젝트에 커스텀 사진 앨범을 구현해야 할 내용이 있다.

내가 담당하게 될지 아닐지는 모르지만, 궁금해서, 살짝 찾아봤다.

일단,  iOS 4 이상에서만 지원이되며,  ALAssetsLibrary 를 통하면 된다.

어렵지 않게 이미지 데이터를 가져올수 있는데,

http://www.icodeblog.com/2010/07/08/asset-libraries-and-blocks-in-ios-4/ 를 참고하였다.
( 블로그 내용안의 동영상이 훨씬 쉬운듯 -_-;;)

1. 프로젝트에 AssetsLibrary.framework 을 포함시킨다.
2. header 에
#import <uikit uikit.h>#import <assetslibrary assetslibrary.h>@interface RootViewController : UITableViewController {    NSMutableArray *assets;}@end
를 추가한다.

여기에서, NSMutableArray *assets 는 포토 앨범에 들어 있는 사진들을 저장할 배열이다. 

3. 이제 포토앨범에 접근하면 되는데, 
- (void)viewDidLoad{    [super viewDidLoad];        assets = [[NSMutableArray alloc] init];    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];        [library enumerateGroupsWithTypes:ALAssetsGroupAll                           usingBlock:^(ALAssetsGroup *group, BOOL *stop) {                               if (group != nil) {                                   [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {                                       if (result!=nil) {                                           NSLog(@"See Asset:%@", result);                                           [assets addObject:result];                                       }                                   }];                               }                                                              [self.tableView reloadData];                           }                         failureBlock:^(NSError *error) {                             NSLog(@"failure: %@", [error description]) ;                         }];}
이렇게 하면, assets 배열에 사진들을 담게 된다.

enumerateGroupsWithTypes:usingBlock:failureBlock:

Invokes a given block passing as a parameter each of the asset groups that match the given asset group type.

- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock

Parameters

types

The types of asset group over which to enumerate.

The value is a bitfield; you can OR together values from “Types of Asset.”

enumerationBlock

The block to invoke using each asset in turn.

When the enumeration is done, enumerationBlock is invoked with group set to nil.

For a description of the block, see ALAssetsLibraryGroupsEnumerationResultsBlock.

failureBlock

The block to invoke if the user denies access to the assets library.

For a description of the block, see ALAssetsLibraryAccessFailureBlock.

Discussion

The results are passed one by one to the caller by executing the enumeration block. When groups are enumerated, the user may be asked to confirm the application's access to the data. If the user denies access to the application, or if no application is allowed to access the data, the failureBlock is called.

Special Considerations

This method will fail with error ALAssetsLibraryAccessGloballyDeniedError if the user has not enabled Location Services (in Settings > General).

Availability

  • Available in iOS 4.0 and later.

Declared In

ALAssetsLibrary.h

제경우, 참고 블로그의 내용대로. safari 에서 이미지를 저장하고 코드를 실행했는데, 아무것도 나오지 않았습니다.

그 이유는  ALAssetsGroupAll 로 주지 않아서인데요.

enum {    ALAssetsGroupLibrary        = (1 << 0),         // The Library group that includes all assets.    ALAssetsGroupAlbum          = (1 << 1),         // All the albums synced from iTunes.    ALAssetsGroupEvent          = (1 << 2),         // All the events synced from iTunes.    ALAssetsGroupFaces          = (1 << 3),         // All the faces albums synced from iTunes.    ALAssetsGroupSavedPhotos    = (1 << 4),         // The Saved Photos album.    ALAssetsGroupAll            = 0xFFFFFFFF,       // The same as ORing together all the available group types.};typedef NSUInteger ALAssetsGroupType;
 ALAssetsGroupType의 타입이 ALAssetsGroupLibrary 로 되어 있어서 였습니다. 나와야 할꺼같은데 안나오는게 참 -_-;;
 

실행 로그는 아래와 같습니다.

2011-09-22 12:03:18.826 AssetLibrary[2102:207] See Asset:ALAsset - Type:Photo, URLs:{

    "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000001&ext=JPG";

}

2011-09-22 12:03:18.828 AssetLibrary[2102:207] See Asset:ALAsset - Type:Photo, URLs:{

    "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000002&ext=JPG";

}

2011-09-22 12:03:18.830 AssetLibrary[2102:207] See Asset:ALAsset - Type:Photo, URLs:{

    "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000003&ext=JPG";

}

2011-09-22 12:03:18.832 AssetLibrary[2102:207] See Asset:ALAsset - Type:Photo, URLs:{

    "public.jpeg" = "assets-library://asset/asset.JPG?id=1000000004&ext=JPG";

}

ALAsset 타입으로 저장되게 되며, API의 Overview에 다음과 같이 설명되어 있다.

An ALAsset object represents a photo or a video managed by the Photo application.

Assets can have multiple representations, for example a photo which was captured in RAW and JPG. Different representations of the same asset may have different dimensions. 

"포토 어플리케이션에 의해 관리되는 사진이나 비디오를 표현하는 오브젝트"

Accessing Properties

  • – valueForProperty:

Accessing Representations

  • – defaultRepresentation
  • – representationForUTI:
  • – thumbnail 
그중 thumbnail 이 중요할 것 같다.

thumbnail

Returns a thumbnail representation of the asset.

- (CGImageRef)thumbnail

Return Value

A thumbnail representation of the asset.

Discussion

The size of the thumbnail is the appropriate for the platform. The image is returned in the correct orientation (that is, “pointing up”—you shouldn’t have to rotate the image).

Availability

  • Available in iOS 4.0 and later.

Declared In

ALAsset.h

CGImageRef 를 리턴해 주는군요.

테이블 뷰에, assets 에 담은 사진 리스트를 보여주려면,

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [assets count];;}// Customize the appearance of table view cells.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"Cell";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];    }        ALAsset *asset = (ALAsset *)[assets objectAtIndex:indexPath.row];    cell.imageView.image = [UIImage imageWithCGImage:[asset thumbnail]];    cell.textLabel.text = [NSString stringWithFormat:@"%@", asset];    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", asset];    // Configure the cell.    return cell;}
CGImageRef를 UIImage로 만들기 위해, [UIImage imageWithCGImage:[asset thumbnail]]; 를 사용했다는 정도? ^^

뭐 어렵지 않게 데이터를 가져오는데 성공했습니다. 하지만, 갤러리로 만들려면, 이제 시작이네요! 후훗!
 
Posted by THLIFE.net

TRACKBACK http://thlife.net/trackback/1075 관련글 쓰기

댓글을 달아 주세요