[10秒学会] - iOS <Photos/Photos.h>自定义相册

这个是完整swift版本Demo: https://github.com/dacaizhao/swiftCameraAlbum 

 

效果图:

点击屏幕 不同坐标显示图片


上代码:

//
//  ViewController.m
//  DCphoto
//
//  Created by point on 16/5/12.
//  Copyright © 2016年 tshiny. All rights reserved.
//

#import "ViewController.h"
#import <Photos/Photos.h>
#import "UIView+YL.h"
@interface ViewController ()
@property(nonatomic,strong) NSMutableArray *imgViewArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imgViewArr = [NSMutableArray array];
    [self searchAllImages];
    
    // Do any additional setup after loading the view, typically from a nib.
}



- (void)searchAllImages {
    // 判断授权状态
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (status != PHAuthorizationStatusAuthorized) return;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // 遍历所有的自定义相册
            PHFetchResult<PHAssetCollection *> *collectionResult0 = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
            for (PHAssetCollection *collection in collectionResult0) {
                [self searchAllImagesInCollection:collection];
            }
            
            // 获得相机胶卷的图片
            PHFetchResult<PHAssetCollection *> *collectionResult1 = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
            for (PHAssetCollection *collection in collectionResult1) {
                if (![collection.localizedTitle isEqualToString:@"Camera Roll"]) continue;
                [self searchAllImagesInCollection:collection];
                break;
            }
        });
    }];
}


- (void)searchAllImagesInCollection:(PHAssetCollection *)collection
{
    // 采取同步获取图片(只获得一次图片)
    PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
    imageOptions.synchronous = YES;
    
    NSLog(@"相册名字:%@", collection.localizedTitle);
    
    // 遍历这个相册中的所有图片
    PHFetchResult<PHAsset *> *assetResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    for (PHAsset *asset in assetResult) {
        // 过滤非图片
        if (asset.mediaType != PHAssetMediaTypeImage) continue;
        
        // 图片原尺寸
        CGSize targetSize = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
        // 请求图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeDefault options:imageOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            int width =  [UIScreen mainScreen].bounds.size.width;
            int height = [UIScreen mainScreen].bounds.size.height;
            CGFloat x = (arc4random() % width) + 0;
            CGFloat y = (arc4random() % height) + 0;
            
            
            
            UIImageView * iView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, 100, 100)];
            [self.view addSubview:iView];
            iView.image = result;
            [_imgViewArr addObject:iView];
            // NSLog(@"图片:%@ %@", result, [NSThread currentThread]);
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UIImageView * img in _imgViewArr) {
        int width =  [UIScreen mainScreen].bounds.size.width;
        int height = [UIScreen mainScreen].bounds.size.height;
        CGFloat x = (arc4random() % width) + 0;
        CGFloat y = (arc4random() % height) + 0;
        
        img.x = x;  //这里  x y 需要坐标扩展  相信大家都知道
        img.y = y;  //!-_-
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

转载于:https://my.oschina.net/zhaodacai/blog/674266

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值