标签:ios
在中级篇里面,已经介绍过相册与拍照的简单使用了,那么接下来我们就来讲一讲它们的后续处理,到底应该怎么做才好呢
首先我们先来讲拍照
还是那个点击使用照片后调用的代理方法中,我们来做以下代码中所提到的事情
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:NULL]; NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType]; UIImage *newImage; WS(weakSelf); if ([mediaType isEqualToString:@"public.image"]) { UIImage *originImage = [info objectForKey:UIImagePickerControllerOriginalImage]; dispatch_sync(dispatch_get_global_queue(0, 0), ^{ [weakSelf saveImageToPhotos:originImage]; }); newImage = [self scaleImage:originImage toScale:0.1]; } [_imageArray addObject:newImage]; }
这个时候,从里面取出的图片是原图,毕竟尺寸、占用内存都比较大,我们需要进行剪切来优化它
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize { UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize)); [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }
当然了,原图我们也是需要保存到相册里面的,所以我们的保存方法如下
- (void)saveImageToPhotos:(UIImage *)savedImage { UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { NSString *message = nil; if (error) { message = @"保存失败"; } else { message = @"保存成功"; [self getAssetsLastPhoto]; } NSLog(@"%@", message); }
这就是拍照后续的使用了,如果还需要优化的话,那就要看你们的产品以及后台如何制定方案了
接下来我们来讲一讲相册吧
最常用的就是我们需要调用一个方法,将所有的照片都取出来,这里要注意的一点是,我们只要缩略图以及原图URL地址就可以了,因为它们两个还是比较重要的
- (void)loadAssets { // Initialise _assets = [NSMutableArray new]; _assetLibrary = [[ALAssetsLibrary alloc] init]; // Run in the background as it takes a while to get all assets from the library dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSMutableArray *assetGroups = [[NSMutableArray alloc] init]; NSMutableArray *assetURLDictionaries = [[NSMutableArray alloc] init]; // Process assets 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 = result.defaultRepresentation.url; [_assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) { if (asset) { @synchronized(_assets) { [_assets addObject:asset]; if (_assets.count == 1) { // Added first asset so reload data // [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; } } } } failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); }]; } } }; // Process groups void (^ assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) { if (group != nil) { [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator]; [assetGroups addObject:group]; } }; // Process! [self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock:^(NSError *error) { NSLog(@"There is an error"); }]; }); }
在显示缩略图的地方,我们可以显示缩略图,而在显示原图的地方,我们也可以根据URL显示原图
@synchronized(_assets) { NSMutableArray *copy = [_assets copy]; for (ALAsset *asset in copy) { [photos addObject:[MWPhoto photoWithThumbImage:[UIImage imageWithCGImage:asset.thumbnail] URL:asset.defaultRepresentation.url]]; } }
上面的方法,就是加入原图的缩略图以及地址URL,这里我用了一个包含两个属性的类来添加,当然你也可以弄两个类来单独添加
另外在上传图片的时候还是需要对原图进行剪切,改变base64,当然还是要看你们服务器那边的方案
好啦,这就是我们对相册以及拍照的后处理咯
就写到这里吧,有什么问题,可以来群里找我哦
本文出自 “东软iOS校友群的技术博客” 博客,请务必保留此出处http://neusoftios.blog.51cto.com/9977509/1650050
标签:ios
原文地址:http://neusoftios.blog.51cto.com/9977509/1650050