2012-09-28 22 views
19

मैं फोटो लाइब्रेरी से सभी तस्वीरें प्राप्त करना चाहता हूं। मैं एक विधि या उदाहरण पसंद करूंगा जिसे मैं सीधे उपयोग कर सकता हूं।एसेट्स लाइब्रेरी फ्रेमवर्क का उपयोग कर एक सरणी में एक आईफोन फोटो लाइब्रेरी से सभी तस्वीरें प्राप्त करें?

+0

एक बार के लिए यह प्रयास करें http://stackoverflow.com/a/38992502/3908884 –

उत्तर

29

// देखें नियंत्रक हैडर (ज) फ़ाइल ..

#import <UIKit/UIKit.h> 
#include <AssetsLibrary/AssetsLibrary.h> 

@interface getPhotoLibViewController : UIViewController 
{ 
ALAssetsLibrary *library; 
NSArray *imageArray; 
NSMutableArray *mutableArray; 
} 

-(void)allPhotosCollected:(NSArray*)imgArray; 

@end 

// कार्यान्वयन फ़ाइल

से तस्वीरें प्राप्त करने के लिए

static int count=0; 

@implementation getPhotoLibViewController 

-(void)getAllPictures 
{ 
imageArray=[[NSArray alloc] init]; 
mutableArray =[[NSMutableArray alloc]init]; 
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init]; 

library = [[ALAssetsLibrary alloc] init]; 

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { 
    if(result != nil) { 
    if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { 
    [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]]; 

    NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

    [library assetForURL:url 
      resultBlock:^(ALAsset *asset) { 
       [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]]; 

       if ([mutableArray count]==count) 
       { 
       imageArray=[[NSArray alloc] initWithArray:mutableArray]; 
       [self allPhotosCollected:imageArray]; 
       } 
      } 
      failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ]; 

    } 
    } 
}; 

NSMutableArray *assetGroups = [[NSMutableArray alloc] init]; 

void (^ assetGroupEnumerator) (ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) { 
    if(group != nil) { 
    [group enumerateAssetsUsingBlock:assetEnumerator]; 
    [assetGroups addObject:group]; 
    count=[group numberOfAssets]; 
    } 
}; 

assetGroups = [[NSMutableArray alloc] init]; 

[library enumerateGroupsWithTypes:ALAssetsGroupAll 
         usingBlock:assetGroupEnumerator 
         failureBlock:^(NSError *error) {NSLog(@"There is an error");}]; 
} 

-(void)allPhotosCollected:(NSArray*)imgArray 
{ 
//write your code here after getting all the photos from library... 
NSLog(@"all pictures are %@",imgArray); 
} 

@end 

उपयोग getAllPicture पद्धति के रूप में वैश्विक गिनती चर घोषित फोटो लाइब्रेरी।

या आप इस ब्लॉग http://mutiselectimagepicker.blogspot.in/2014/08/imageselect-to-allow-multiple-selection.html

+1

उपरोक्त दृष्टिकोण की स्मृति पदचिह्न जबरदस्त है! हालांकि यह करने का एक बहुत ही उपयुक्त और सरल तरीका है। एसिंक्रोनस ब्लॉक के उपयोग के कारण मेमोरी प्रबंधन विशेष रूप से मुश्किल हो जाता है। –

+1

यह त्रुटि देता है, अगर हम का उपयोग करें "# शामिल " तो अपने शीर्षक में इस का उपयोग करें किसी भी त्रुटि पदच्युत करने के लिए: #import सहायता के लिए: http : //stackoverflow.com/questions/10342931/alasset-valueforpropertyalassetpropertylocation-undeclared –

+0

आयात #import से अधिक के लिए इस त्रुटि आ: उदाहरण संदेश के लिए रिसीवर प्रकार 'ALAssetRepresentation' एक आगे घोषणा –

0

-(void)getFromGallery:(BOOL)IsImages 
 
{ 
 
    if(self.csCollectionsArray != nil) 
 
     [self.csCollectionsArray removeAllObjects]; 
 
    __block NSMutableDictionary *date = [[NSMutableDictionary alloc] init]; 
 
    ALAssetsLibrary *csAssetsLibrary = [[ALAssetsLibrary alloc] init]; 
 
    NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos; 
 
    [csAssetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
 
    { 
 
     if([group numberOfAssets] > 0) 
 
     { 
 
      if(IsImages) 
 
       [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 
 
      else 
 
       [group setAssetsFilter:[ALAssetsFilter allVideos]]; 
 
      [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) 
 
      { 
 
       if(asset) 
 
       { //1.fetching all assets from device library 
 
        //2.Add all fetched assests from library 
 
        [date setObject:asset forKey:[asset valueForProperty:ALAssetPropertyDate]]; 
 
       } 
 
      }]; 
 
     } 
 
     else 
 
     { NSLog(@"---> load table -------->"); 
 
      if(date != nil && date.count > 0) 
 
      { //3.Sort using date by ascending order and moved to dictionary to array 
 
       NSArray *sortedKeys = [[date allKeys] sortedArrayUsingSelector: @selector(compare:)]; 
 
       for (NSString *key in sortedKeys) 
 
        [self.csCollectionsArray addObject: [date objectForKey:key]]; 
 
       //4.Load images into collection view after fetching all datas 
 
       [self reloadCollectionView]; 
 
       if(self.csCollectionView != nil) 
 
        [self.csCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:([self.csCollectionsArray count] - 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES]; 
 
      } 
 
      date = nil; 
 
     } 
 
    }failureBlock:^(NSError *error) 
 
    { 
 
     if((csCollectionsArray == nil || [csCollectionsArray count] == 0)) 
 
     { 
 
      ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; if(status != ALAuthorizationStatusAuthorized) 
 
      { 
 
       [self showAlertAndCloseUploaderView:@"You can just go to \"Settings\" app (General -> Reset -> Reset Location & Privacy) then come again and click ok when the alert dialog is showing for enable the permission to access the photo library"]; 
 
      } 
 
     } 
 
    }]; 
 
}

आप ios में संपत्ति पुस्तकालय से सभी छवियों या वीडियो लाने के लिए विधि नीचे इस का उपयोग कर सकते है पर एक नज़र हो सकता है। उपयोग इस assetslibrary फ्रेमवर्क (चाहिए) नोट: - #import

-(void)getFromGallery:(BOOL)IsImages 
{ 
    if(self.csCollectionsArray != nil) 
     [self.csCollectionsArray removeAllObjects]; 
    __block NSMutableDictionary *date = [[NSMutableDictionary alloc] init]; 
    ALAssetsLibrary *csAssetsLibrary = [[ALAssetsLibrary alloc] init]; 
    NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos; 
    [csAssetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
    { 
     if([group numberOfAssets] > 0) 
     { 
      if(IsImages) 
       [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 
      else 
       [group setAssetsFilter:[ALAssetsFilter allVideos]]; 
      [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) 
      { 
       if(asset) 
       { //1.fetching all assets from device library 
        //2.Add all fetched assests from library 
        [date setObject:asset forKey:[asset valueForProperty:ALAssetPropertyDate]]; 
       } 
      }]; 
     } 
     else 
     { NSLog(@"---> load table -------->"); 
      if(date != nil && date.count > 0) 
      { //3.Sort using date by ascending order and moved to dictionary to array 
       NSArray *sortedKeys = [[date allKeys] sortedArrayUsingSelector: @selector(compare:)]; 
       for (NSString *key in sortedKeys) 
        [self.csCollectionsArray addObject: [date objectForKey:key]]; 
       //4.Load images into collection view after fetching all datas 
       [self reloadCollectionView]; 
       if(self.csCollectionView != nil) 
        [self.csCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:([self.csCollectionsArray count] - 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES]; 
      } 
      date = nil; 
     } 
    }failureBlock:^(NSError *error) 
    { 
     if((csCollectionsArray == nil || [csCollectionsArray count] == 0)) 
     { 
      ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; if(status != ALAuthorizationStatusAuthorized) 
      { 
       [self showAlertAndCloseUploaderView:@"You can just go to \"Settings\" app (General -> Reset -> Reset Location & Privacy) then come again and click ok when the alert dialog is showing for enable the permission to access the photo library"]; 
      } 
     } 
    }]; 
} 
6

ALAssetsLibrary के बाद से अब हटा दिया गया है और Photo Framework नया एक है। मैं NSArray में कैमरा रोल और दुकान से सभी लोगों के लिए बनाए Objective C में अपने ही समारोह बना दिया है और मेरी Collectionview में दिखाया गया है ..

NSArray *imageArray; 
NSMutableArray *mutableArray; 

-(void)getAllPhotosFromCamera 
{ 
    imageArray=[[NSArray alloc] init]; 
    mutableArray =[[NSMutableArray alloc]init]; 

    PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init]; 
    requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact; 
    requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; 
    requestOptions.synchronous = true; 
    PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil]; 

    NSLog(@"%d",(int)result.count); 

    PHImageManager *manager = [PHImageManager defaultManager]; 
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[result count]]; 

    // assets contains PHAsset objects. 

    __block UIImage *ima; 
    for (PHAsset *asset in result) { 
     // Do something with the asset 

     [manager requestImageForAsset:asset 
          targetSize:PHImageManagerMaximumSize 
          contentMode:PHImageContentModeDefault 
           options:requestOptions 
         resultHandler:^void(UIImage *image, NSDictionary *info) { 
          ima = image; 

          [images addObject:ima]; 
         }]; 


    } 

    imageArray = [images copy]; // You can direct use NSMutuable Array images 
} 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^