您的位置:首页 > 移动开发 > IOS开发

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

2017-01-11 16:59 471 查看
摘要: [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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息