วันจันทร์ที่ 6 มิถุนายน พ.ศ. 2554

Code iPhone: Load Photo from Photos Album (Full Function)

Code iPhone: Load Photo from Photos Album (Full Function)

In .h file (header/@interface)

[sourcecode language="objc"]

#import <AssetsLibrary/AssetsLibrary.h>

typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *myasset);

typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);

//function

-(void)loadPhotoFromURL:(NSURL*)imgURL thumbnail:(BOOL)useThumbnail showIn:(UIImageView*)imView;

[/sourcecode]

Note! import existing framework: AssetsLibrary.framework



In .m file (@implement)

[sourcecode language="objc"]

-(void)loadPhotoFromURL:(NSURL*)imgURL thumbnail:(BOOL)useThumbnail showIn:(UIImageView*)imView{

if (imgURL!=nil) {

ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myasset){

CGImageRef iref;

if (useThumbnail) {

iref = [myasset thumbnail];

}else {

ALAssetRepresentation *rep = [myasset defaultRepresentation];

iref = [rep fullScreenImage];

}

if (iref) {

UIImage *resPhoto = [UIImage imageWithCGImage:iref];

//NSLog(@"photo size:%f x %f",resPhoto.size.width,resPhoto.size.height);

imView.image = resPhoto;

}

};//end result block


ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){

NSLog(@"error....");

};//end failureBlock


ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease];

[assetLib assetForURL:imgURL

resultBlock:resultBlock

failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock];

}//end if

}//end function

[/sourcecode]

Relate Article

adaydesign :)

ไม่มีความคิดเห็น:

แสดงความคิดเห็น