繁体   English   中英

如何使用ALAssets获取设备中的所有图像?

[英]How to get all images in device with ALAssets?

我是新的iOS开发人员,我正在尝试获取设备中的所有图像。 它在模拟器和iPhone 4,4s(iOS 6.0-> 7.1)上运行良好,但是在iPhone 5s iOS 7.04上,我无法获取图像。 它总是返回false。 这是我的代码:

self.assetGroups = [NSMutableArray array];

[[ALAssetsManager defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        [self.assetGroups addObject:group];
    } else {
        // this is the end
        [self displayPickerForGroup];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"failse cmnr");
}];

- (void)displayPickerForGroup {}

并在ALAsetsManager.m

#import "ALAssetsManager.h"

@implementation ALAssetsManager
+ (ALAssetsLibrary *)defaultAssetsLibrary {
    static dispatch_once_t pred = 0;
    static ALAssetsLibrary *library = nil;
    dispatch_once(&pred, ^{
        library = [[ALAssetsLibrary alloc] init];
    });
    return library;
}
@end

我的代码有什么问题?

note: Displaying Images from Iphone Photo Gallery into UICollection View .

**Step1:** Import these UIFrameworks AssetsLibrary.framework and  Photo.framework  ...

**Step 2:** viewControl.h

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

@interface ImageLibrary : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>

{
   NSMutableArray *allAssetArray;
}

@property (strong, nonatomic) IBOutlet UICollectionView *myCollectionView;

@property (strong, nonatomic) IBOutlet UIImageView *largeImage;
@property (strong, nonatomic) IBOutlet UIView *myView;
@property (strong, nonatomic) IBOutlet UIButton *btnCloseOutlet;
- (IBAction)btnCloseAction:(id)sender;
@end

**Step 3:**

#import "viewControl.h"
#import "MyLibraryCell.h"
@interface ImageLibrary ()

@end

@implementation ImageLibrary
@synthesize myCollectionView;
@synthesize myView,largeImage;
@synthesize btnCloseOutlet;
- (void)viewDidLoad {
    [super viewDidLoad];

    [self.myCollectionView registerNib:[UINib nibWithNibName:@"MyLibraryCell" bundle:Nil] forCellWithReuseIdentifier:@"CELL"];


    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setItemSize:CGSizeMake(160, 120)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.myCollectionView setCollectionViewLayout:flowLayout];

    allAssetArray = [[NSMutableArray alloc]init];
    NSLog(@"count is %lu ",(unsigned long)allAssetArray.count);


    [self findLargeImage];
    NSLog(@"count is %lu ",(unsigned long)allAssetArray.count);

    self.title = @"From Album";

    self.myView.alpha = 0.0f;
    btnCloseOutlet.layer.cornerRadius = btnCloseOutlet.bounds.size.width/2;
    btnCloseOutlet.layer.borderColor=[UIColor blackColor].CGColor;
    btnCloseOutlet.layer.borderWidth=2.0f;

    self.myCollectionView.userInteractionEnabled = YES;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


#pragma mark - Number of Intems

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"count is %lu ",(unsigned long)allAssetArray.count);
    return allAssetArray.count;
}

#pragma mark - Number Of Secions

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{
    return 1;
}


#pragma mark - CellForItemAtIndexPath
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyLibraryCell *cell = (MyLibraryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];   
    NSLog(@"value is %@ ",[allAssetArray objectAtIndex:indexPath.row]);
    [[PHImageManager defaultManager] requestImageForAsset:[allAssetArray objectAtIndex:indexPath.row]
                                               targetSize:cell.myImage.bounds.size
                                              contentMode:PHImageContentModeAspectFill
                                                  options:PHImageRequestOptionsVersionCurrent
                                            resultHandler:^(UIImage *result, NSDictionary *info) {

                                                dispatch_async(dispatch_get_main_queue(), ^{
                                                    cell.myImage.image = result;
                                                });
                                            }];


    return cell;
}


#pragma mark collection view cell paddings
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

    return 4.0;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    self.myCollectionView.userInteractionEnabled = NO;

    [UIView animateWithDuration:0.4
                          delay: 0.2
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         myView.alpha = 0.5;
                         myView.backgroundColor = [UIColor lightGrayColor];
                     }
                     completion:^(BOOL finished){
                         [UIView animateWithDuration:0.6 animations:^{
                             myView.frame = CGRectMake(10, 80, self.view.frame.size.width-20, self.view.frame.size.height-100);
                             myView.alpha = 1.0;
                             myView.backgroundColor = [UIColor whiteColor];
                         }];
                     }];



    [[PHImageManager defaultManager] requestImageForAsset:[allAssetArray objectAtIndex:indexPath.row]
targetSize:self.largeImage.bounds.size
contentMode:PHImageContentModeAspectFill
options:PHImageRequestOptionsVersionCurrent
resultHandler:^(UIImage *result, NSDictionary *info) {

    dispatch_async(dispatch_get_main_queue(), ^{
        self.largeImage.image = result;
    });
}];

}



-(void)findLargeImage
{
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

    PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
    [result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
        [allAssetArray addObject:asset];
    }];




}

- (IBAction)btnCloseAction:(id)sender {
    self.myCollectionView.userInteractionEnabled = YES;

    [UIView animateWithDuration:0.4
                          delay: 0.2
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         myView.alpha = 0.5;
                         myView.backgroundColor = [UIColor lightGrayColor];
                     }
                     completion:^(BOOL finished){
                         [UIView animateWithDuration:0.4 animations:^{
                             myView.alpha = 0.0;

                         }];
                     }];


}
@end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM