OC简单实现本地图片保存

OC简单实现本地图片保存

首先导入头文件

#import <Photos/PHPhotoLibrary.h>

Ps: 不要忘记这个 NSPhotoLibraryAddUsageDescription

然后下载方法

- (void)buttonDownloadTarget{

//判断是不是可以下载

    BOOL isSave =  [self isCanDown];

    __weak typeof(self) weakSelf = self;

    if (isSave) {

        // 保存图片到相册中        UIImageWriteToSavedPhotosAlbum(self.imageViewSave.image,self, @selector(image:didFinishSavingWithError:contextInfo:),nil);

    }else{

     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message: @"是否去开启相册权限" preferredStyle:UIAlertControllerStyleAlert];

    [self presentViewController:alertController animated:YES completion:nil];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

  [weakSelf openSet];​​​​​​​

    }];

    UIAlertAction *actionS = [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    

    }];

  

    [alertController addAction:action];

[alertController addAction:actionS];

          }   

}

//保存图片完成之后的回调 这个必须有

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

  contextInfo:(void *)contextInfo

{

    // Was there an error?

    if (error != NULL)

{

NSLog(@"下载失败")       

    }

    else

{

NSLog(@"下载成功")     

    }

}

//开启相册权限 直接跳过去

-(void)openSet{

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

if ([[UIApplication sharedApplication] canOpenURL:url]) {

[[UIApplication sharedApplication] openURL:url];

}

}

//判断是不是可以下载

- (BOOL) isCanDown {

        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

        if (status == PHAuthorizationStatusRestricted ||

            status == PHAuthorizationStatusDenied) {

            //无权限

            return NO;

        }

//有权限

    return YES;

}

 - (void)textFieldEditChanged:(UITextField *)textField

{

        [self textFieldDidChange:textField interger:10];   

}

干货就是这么简单,点个赞再走呀亲!!!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用CFNetwork框架来实现FTP上传图片。下面是一个简单的示例代码: ``` - (void)uploadImageToFTP:(UIImage *)image { // FTP服务器地址、用户名、密码 NSString *ftpUrl = @"ftp://ftp.example.com"; NSString *userName = @"username"; NSString *password = @"password"; // 生成图片的NSData NSData *imageData = UIImageJPEGRepresentation(image, 1.0); // 生成文件名 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]]; // 生成FTP文件路径 NSString *ftpFilePath = [NSString stringWithFormat:@"%@/%@", ftpUrl, fileName]; // 生成FTP URL CFURLRef ftpURL = CFURLCreateWithString(NULL, (__bridge CFStringRef)ftpFilePath, NULL); // 生成FTP Write Stream CFWriteStreamRef writeStream = CFWriteStreamCreateWithFTPURL(NULL, ftpURL); // 设置FTP用户名和密码 CFWriteStreamSetProperty(writeStream, kCFStreamPropertyFTPUserName, (__bridge CFStringRef)userName); CFWriteStreamSetProperty(writeStream, kCFStreamPropertyFTPPassword, (__bridge CFStringRef)password); // 打开FTP Write Stream if (CFWriteStreamOpen(writeStream)) { // 写入数据 const uint8_t *buffer = [imageData bytes]; CFIndex bytesWritten = CFWriteStreamWrite(writeStream, buffer, [imageData length]); // 关闭FTP Write Stream CFWriteStreamClose(writeStream); if (bytesWritten == [imageData length]) { NSLog(@"FTP上传成功"); } else { NSLog(@"FTP上传失败"); } } else { NSLog(@"FTP连接失败"); } // 释放资源 CFRelease(writeStream); CFRelease(ftpURL); } ``` 在上面的代码中,我们首先定义了FTP服务器地址、用户名和密码,然后生成了要上传的图片的NSData、文件名、FTP文件路径和FTP URL。接着,我们使用CFWriteStreamCreateWithFTPURL函数创建了FTP Write Stream,并设置了FTP用户名和密码。然后,我们打开FTP Write Stream,并将图片数据写入到FTP服务器上。最后,我们检查数据是否写入成功,并释放资源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值