UIimage图片在程序Documents目录下的存取

1、把图片存在Documents目录下

    

if (image!=nil) {        

        NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

        NSString *uniquePath=[[paths objectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:@"image%d.png",xxx]];

        BOOL result = [UIImagePNGRepresentation(imagewriteToFile:uniquePathatomically:YES];

        if (result) {

            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@""message:@"图片保存成功!" delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil];

            [alert show];

            [alert release];

        }

    }else {

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"图片保存失败!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [alert show];

        [alert release];

    }  


2、从Documents目录下取出.png图片


NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMask,YESlastObject];

    NSArray *dirContents = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDir error:nil];

    NSArray *onlyPics = [dirContents filteredArrayUsingPredicate:[NSPredicatepredicateWithFormat:@"self ENDSWITH '.png'"]];


3、从Documents目录中删除


            NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMask,YES); 

            NSString *documentsDirectory = [paths objectAtIndex:0]; 

            NSString *filePath = [documentsDirectorystringByAppendingPathComponent:这是图片的名称/哪一张图片];

            BOOL succeed = [[NSFileManager defaultManager]removeItemAtPath:filePath error:nil];


            if (succeed) {

                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@""message:@"图片删除成功!" delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil];

                [alert show];

                [alert release];

                [navigation popViewControllerAnimated:YES];  


            }


4、保存到相册


 NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

            NSString *documentDir = [paths objectAtIndex:0];

            NSString *imgPath=[documentDir stringByAppendingPathComponent:[pageArray objectAtIndex:pageTag]];

            UIImage *img=[UIImage imageWithContentsOfFile:imgPath];

            UIImageWriteToSavedPhotosAlbum(img, self,@selector(image:didFinishSavingWithError:contextInfo:), nil);


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    if (error != NULL)

    {

    }else  // No errors

    {

        [self saveSucceed];

    }

}


- (void)saveSucceed {

    UIAlertView *sucAlert = [[UIAlertView allocinitWithTitle:@""message:@"图片已保存到相册" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];

    [sucAlert show];

    [sucAlert release];

    self.navigationItem.leftBarButtonItem.enabled = YES;

    self.navigationItem.rightBarButtonItem.enabled = YES;

}


转载于:https://www.cnblogs.com/zsw-1993/archive/2012/11/06/4880663.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值