iOS获取所有照片

最近的项目要求做一个照片浏览器,之前没做过,在做的过程中遇到的第一个紧要问题就是 “如何获取手机相册所有的照片”,在网上看了下资料,实现过程基本是这样的:


1.首先导入框架   ASSetsLibrary.framework

然后,#import <AssetsLibrary/AssetsLibrary.h>


2.获取所有照片


//获取所有的图片

- (void)getAllImage{

    //获取失败

    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){

        MCLog(@"相册访问失败 =%@", [myerror localizedDescription]);

        if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {

            MCLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");

        }else{

            MCLog(@"相册访问失败.");

        }

    };

    

    //获取到的图片

    ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){

        if (result!=NULL) {

            if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {

                //图片的url

                NSString *urlstr=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url];


//把获取到的照片地址存储到自己想要的数组中

                [self.imageArr addObject:urlstr];

            }

            [self.collectionView reloadData];

        }

    };

    

    ALAssetsLibraryGroupsEnumerationResultsBlock

    libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){

        if (group == nil){}

        

        if (group!=nil) {

            NSString *g=[NSString stringWithFormat:@"%@",group];//获取相簿的组

            MCLog(@"gg:%@",g);//gg:ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:71

            NSString *g1=[g substringFromIndex:16 ] ;

            NSArray *arr=[[NSArray alloc] init];

            arr=[g1 componentsSeparatedByString:@","];

            NSString *g2=[[arr objectAtIndex:0] substringFromIndex:5];

            if ([g2 isEqualToString:@"Camera Roll"]) {

                g2=@"相机胶卷";

            }

//            NSString *groupName=g2;//组的name

            [group enumerateAssetsUsingBlock:groupEnumerAtion];

        }

    };

    

    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll

                           usingBlock:libraryGroupsEnumeration

                         failureBlock:failureblock];

}



//获取图片

    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];

    //从之前的数组中拿出照片的url

    NSString *urlStr = self.imageArr[indexPath.item];

    NSURL *url=[NSURL URLWithString:urlStr];

    [assetLibrary assetForURL:url resultBlock:^(ALAsset *asset)  {

        //这里可以设置照片的模式,好像只有两个:缩略图、等比缩略图

        UIImage *image=[UIImage imageWithCGImage:asset.aspectRatioThumbnail];

        cell.icon.image=image;

    }failureBlock:^(NSError *error) {

        NSLog(@"error=%@",error);

    }

     ];



好了,大功告成,写的不好,希望各位多多指教。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在iOS开发中获取图片中二维码的定位,可以使用CoreImage框架。具体步骤如下: 1. 使用CIImage加载图片: ``` CIImage *image = [CIImage imageWithCGImage:image.CGImage]; ``` 2. 创建CIDetector并设置识别类型: ``` CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}]; ``` 3. 识别二维码: ``` NSArray *features = [detector featuresInImage:image]; ``` 4. 遍历识别结果,获取二维码定位: ``` for (CIQRCodeFeature *feature in features) { NSArray *corners = feature.corners; // 获取定位四个角的坐标 // 在图像上绘制定位 UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor); CGContextSetLineWidth(context, 2.0); CGContextMoveToPoint(context, corners[0].x, corners[0].y); CGContextAddLineToPoint(context, corners[1].x, corners[1].y); CGContextAddLineToPoint(context, corners[2].x, corners[2].y); CGContextAddLineToPoint(context, corners[3].x, corners[3].y); CGContextAddLineToPoint(context, corners[0].x, corners[0].y); CGContextStrokePath(context); UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } ``` 这样就可以在原图上绘制出二维码的定位了。需要注意的是,CIDetector只能识别二维码,如果要识别其他类型的码,需要设置不同的detector类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值