我想訪問並將我iPhone相冊中的一個相冊中的所有照片複製到我的應用程序的文檔文件夾AUTOMATICALLY當APP啓動時。如何複製iOS上的應用程序的文檔文件夾中的所有照片?
這裏有很多關於使用UIImagePicker的問題/答案。我知道我可以用圖像選擇器來做,並讓用戶選擇照片,但如果我可以在沒有用戶選擇照片的情況下做到這一點,我會非常喜歡它。
我已經使用了以下問題/答案,以幫助下面的代碼:How to retrieve the most recent photo from Camera Roll on iOS?
我能夠成功挽救一個形象在我的文件夾,但無法弄清楚如何節省超過一次一個。
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes: ALAssetsGroupAlbum //ALAssetsGroupAlbum before I CHANGED it
usingBlock:^(ALAssetsGroup *group, BOOL *stop) { //
if (nil != group) {
// be sure to filter the group so you only get photos
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex: group.numberOfAssets - 1 ]// -1
options:0
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (nil != result)
{
ALAssetRepresentation *repr = [result defaultRepresentation];
// this is the most recent saved photo
UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];
// we only need the first (most recent) photo -- stop the enumeration
// *stop = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
int num = 1;
NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%@-%d.png",@"boxOneImage", num]];
num++;
NSData* data = UIImagePNGRepresentation(img);
[data writeToFile:path atomically: YES];
}
}];
}
*stop = NO;
} failureBlock:^(NSError *error) {
NSLog(@"error: %@", error);
}];
更新:我能弄清楚,而不是相機膠捲到我的應用程序文件夾使用下面的代碼只是一個相冊載入圖片:
__block int num = 1;
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (nil != group)
{
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
if (nil != result)
{
ALAssetRepresentation *repr = [result defaultRepresentation];
// this is the most recent saved photo
UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%@-%d.png",@"boxOneImage", num]];
num++;
NSData* data = UIImagePNGRepresentation(img);
[data writeToFile:path atomically: YES];
}
}];
}
*stop = NO;
} failureBlock:^(NSError *error)
{
NSLog(@"error: %@", error);
}];
如果有人或知道一個更好的方式做到這一點,然後請分享。我很樂意看到它並從中學習。謝謝!!!
引用此:http://stackoverflow.com/questions/12633843/get-all-of-the-pictures-from-an-iphone-photolibrary-in-an-array-using-assetslibr – Mayur