iOS 相册备份功能实现
功能实现起来也不是很难,比对的话我用的MD5 ,进行比对文件是否重复,是否已上传 ,或未上传。MD5反正算是个问题,解决的办法不是很好,我会改进,因为功能急,暂时没有好的办法,只能 这么来了。
if (isAutoBackupOfVideo||isAutoBackupOfPhoto) {
if (!self.assetsLibrarry) {
self.assetsLibrarry=[[ALAssetsLibrary alloc]init];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *groups=[NSMutableArray array];
[self.assetsLibrarry enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[groups addObject:group];
}else{
for (ALAssetsGroup *singeGroup in groups) {
NSString *albumName=[singeGroup valueForProperty:ALAssetsGroupPropertyName];
NSMutableArray *photos=[NSMutableArray array];
NSMutableArray *videos=[NSMutableArray array];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[singeGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
if (IsEquallString(ALAssetTypePhoto, [result valueForProperty:ALAssetPropertyType])) {
if (isAutoBackupOfPhoto) {
Photo *photo=[[Photo alloc]init];
photo.url=[[[result defaultRepresentation] url] absoluteString];
photo.album=albumName;
photo.thumbnail=[UIImage imageWithCGImage:[result thumbnail]];
photo.name=[[result defaultRepresentation] filename];
photo.type=[photo.name pathExtension];
photo.size=[NSString stringWithFormat:@"%lld",[[result defaultRepresentation] size]];
[photos addObject:result];
}
}
if (IsEquallString(ALAssetTypeVideo, [result valueForProperty:ALAssetPropertyType])) {
if (isAutoBackupOfVideo) {
Video *video=[[Video alloc]init];
video.url=[[[result defaultRepresentation] url] absoluteString];
video.album=albumName;
video.name=[[result defaultRepresentation] filename];
video.type=[video.name pathExtension];
video.thumbnail=[UIImage imageWithCGImage:[result thumbnail]];
video.size=[NSString stringWithFormat:@"%lld",[[result defaultRepresentation] size]];
[videos addObject:result];
}
}
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (photos.count) {
[self uploadPhoto:photos];
}
if (videos.count) {
[self uploadVideos:videos];
}
});
}
}];
});
}
}
} failureBlock:^(NSError *error) {
}];
});
}
//获得本地图片的MD5数组
-(void)getLocalPhotos:(getLocalPhotosSuccessBlock)block{
if (!self.assetsLibrarry) {
self.assetsLibrarry=[[ALAssetsLibrary alloc]init];
}
NSMutableArray *groups=[NSMutableArray array];
[self.assetsLibrarry enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[groups addObject:group];
}else{
//
for (ALAssetsGroup *singeGroup in groups) {
NSString *albumName=[singeGroup valueForProperty:ALAssetsGroupPropertyName];
NSMutableArray *photos=[NSMutableArray array];//存储ALAsset对象
NSMutableArray * photoArr = [NSMutableArray array];//存储photo对象
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[singeGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
if (IsEquallString(ALAssetTypePhoto, [result valueForProperty:ALAssetPropertyType])) {
Photo *photo=[[Photo alloc]init];
photo.url=[[[result defaultRepresentation] url] absoluteString];
photo.album=albumName;
photo.thumbnail=[UIImage imageWithCGImage:[result thumbnail]];
photo.name=[[result defaultRepresentation] filename];
photo.type=[photo.name pathExtension];
photo.md5 =[self getImageMD5WithUrl:result];
NSDate * date = [result valueForProperty:ALAssetPropertyDate];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
dateformatter.dateFormat=@"YYYY-MM-dd HH:mm:ss";
photo.lastWriteTime = [dateformatter stringFromDate:date];
photo.size=[NSString stringWithFormat:@"%lld",[[result defaultRepresentation] size]];
photo.result = result;
[photos addObject:result];
[photoArr addObject:photo];
}
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (photoArr.count) {
block(photoArr);
}
});
}
}];
});
}
//
}
} failureBlock:^(NSError *error) {
}];
}
//写入相册
-(void)storeFinishByTypeFile:(TypeFile*)typeFile andIsSuccess:(BOOL)isSuccess{
if (isSuccess) {
NSString *path=[self getFilePathWithFile:typeFile];
NSData *imageData=[NSData dataWithContentsOfFile:path];
UIImage *image=[UIImage imageWithData:imageData];
if (!self.assetsLibrarry) {
self.assetsLibrarry = [[ALAssetsLibrary alloc]init];
}
[self.assetsLibrarry writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL* assetURL, NSError* error) {
if (error!=nil) {
[Utils showAlertViewByText:[NSString stringWithFormat:@"%@保存失败",typeFile.name]];
}else{
typeFile.url=[assetURL description];
NSLog(@"Success--%@",typeFile.url);
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
}];
}else{
[Utils showAlertViewByText:[NSString stringWithFormat:@"%@保存失败",typeFile.name]];
}
}
for (ALAsset *asset in photos) {
NSString * MD5Str = [self getImageMD5WithUrl:asset ];
if (MD5Str.length>0) {
[[RequestIP shardRequestIP].localMd5Arr addObject:MD5Str];
if([MD5Arr indexOfObject:MD5Str]== NSNotFound){
NSLog(@"NSNotFound!");
if ([uploadArr indexOfObject:MD5Str] == NSNotFound) {
[uploadArr addObject:MD5Str];
NSLog(@"MD5Str:%@",MD5Str);
if (!IS_AUTO_CopyPhotos) {
break;
}