图片保存在相册中-相同图片(地址)避免重复保存

网络加载-相同图片也就是图片的地址相同,所以处介绍的是图片地址相同的方法,如果拍照图片,同理;试着想用对图片解码处理。。这个应该可以扩展

使用方法 ManagerImagesLocal保存图片的类


[ManagerImagesLocal pathForImage:param withComplateHandle:^ {
            NSLog(@"已保存过了");

    } onFial:^(BOOL saveFail) {
        if (!saveFail) {
            NSLog(@"保存成功");
       
        }else {
            NSLog(@"保存失败");
           
        }
    }];



+(void)pathForImage:(NSString *)theImgStr withComplateHandle:(void (^)(void))successHandler onFial:(void (^)(BOOL saveFail))aFail

{
    //copy
//    [ManagerImagesLocal shareFactory].yk_failHander=aFail;
    //指定新建文件夹路径
    NSString *imageDocPath = [self dataFilePath];
    //获取文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isFlag;
    
    __block BOOL findImageIsHas = YES;
    
    if ([fileManager fileExistsAtPath:imageDocPath isDirectory:&isFlag]) {
        
        NSMutableDictionary *dicfiel = [NSMutableDictionary dictionaryWithContentsOfFile:imageDocPath];
        
        if (dicfiel && ![[dicfiel objectForKey:theImgStr] isEqualToString:@""]) {
            
            __block NSURL *asseturl = [NSURL URLWithString:[dicfiel objectForKey:theImgStr]];
            
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            
            NSLog(@"asset url %@", asseturl);
            
            // Try to load asset at mediaURL  // find image is exit or not
            [library assetForURL:asseturl resultBlock:^(ALAsset *asset) {
                
                // If asset doesn't exists
                if (!asset){
                    NSLog(@"Could not find asset  delete from photo library ");
                    
//                    fialHandler();
                    
                    findImageIsHas = NO;
                    [self setBark:theImgStr onFial:aFail];
//                    successHandler(NO);
                }else{
                    
                    successHandler();
                    NSLog(@"tmpListAsset : %@", asset);
                    
                }
            } failureBlock:^(NSError *error) {
                // Type your code here for failure (when user doesn't allow location in your app)
//                fialHandler();
                findImageIsHas = NO;
                NSLog(@"error :%@",error.description);
                [self setBark:theImgStr onFial:aFail];
            }];
            
            
            
        }else {
            findImageIsHas = NO;
//            fialHandler();
            [self setBark:theImgStr onFial:aFail];
            
        }
    }else {
        findImageIsHas = NO;
//        fialHandler();
        [self setBark:theImgStr onFial:aFail];
    }
    
        // 试着相同的方法只写一次的处理,这里有点累赘   

}



// 不存在的时候保存图片--



+(NSError *) setBark:(NSString *)urlStr onFial:(void (^)(BOOL isSaved))fialHandler
{
    __block NSError * errorReturn = nil;
    
    NSData *dataimage = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    UIImage *image = [UIImage imageWithData:dataimage];
    
    ALAssetsLibrary *assetLib = [[ALAssetsLibrary alloc] init];
        
    [assetLib writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
        
        
        [assetLib release];
        
        if (error == nil) {
            
            NSLog(@"save image successed: %@",[assetURL description]);
            [self saveImageUrl:[assetURL description] forKey:urlStr];
//            [ManagerImagesLocal shareFactory].yk_failHander(YES);
            fialHandler(NO);
        }else {
            
            NSLog(@"save image field: %@",assetURL);
            
            errorReturn = error;
            fialHandler(YES);
        }
        
    }];

    return errorReturn;
    
}


// 附加方法


+(void)saveImageUrl:(NSString *)imgUrl forKey:(NSString *)key
{
    NSString *imageDocPath = [self dataFilePath];//[documentPath stringByAppendingPathComponent:@"LocaImageField"];
    //获取文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isFlag;
    if ([fileManager fileExistsAtPath:imageDocPath isDirectory:&isFlag]) {
        NSMutableDictionary *dicfiel = [NSMutableDictionary dictionaryWithContentsOfFile:imageDocPath];
        [dicfiel setObject:imgUrl forKey:key];
        [dicfiel writeToFile:imageDocPath atomically:YES];
    }else {
        NSDictionary *dicSave = [NSDictionary dictionaryWithObjectsAndKeys:imgUrl,key, nil];
        [dicSave writeToFile:imageDocPath atomically:YES];
    }
}

+(NSString *)dataFilePath:(NSString *)fieldPath
{
    //获取Documents文件夹目录
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory=[paths objectAtIndex:0];
    //指定新建文件夹路径
    return [documentDirectory stringByAppendingPathComponent:fieldPath];
}
+(NSString *)dataFilePath
{
    return [self dataFilePath:@"LocaImageField"];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android,我们可以将图片保存相册。下面是实现的步骤: 1. 首先,我们需要获取到要保存图片的Bitmap对象。 你可以从网络、本地文件或者其他资源获取到图片的Bitmap对象。 2. 接下来,我们需要创建一个文件对象来保存图片。我们将使用Environment的DIRECTORY_DCIM目录作为保存图片的目录。 ```java String fileName = "my_image.jpg"; File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File imageFile = new File(storageDir, fileName); ``` 3. 确保我们的App具有写入外部存储的权限。在AndroidManifest.xml文件添加以下权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ``` 4. 接下来,我们需要把Bitmap对象保存到文件。我们可以使用Bitmap的compress()方法来实现这一点。 ```java try { FileOutputStream fos = new FileOutputStream(imageFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } ``` 5. 最后一步是通知系统相册更新图片库,以便于在相册显示我们保存图片。 ```java Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(imageFile); mediaScanIntent.setData(contentUri); sendBroadcast(mediaScanIntent); ``` 这样,我们就成功将图片保存相册了。你可以在系统相册找到保存图片。请注意,Android 10及以上版本需要特殊处理来保存相册

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值