iOS--更換圖片并保存

更換圖片的方法有很多,現(xiàn)在對其中一種方式進行學習,加深自己的知識點.
一.更換圖片
利用手勢點擊圖片調(diào)用系統(tǒng)相機和系統(tǒng)相冊更換圖片的需求簡單處理.
1.展示一張本地圖片的代碼

//設(shè)置圖像的一些屬性,添加手勢
- (void)setImageViewConfiguration{
    
    self.changeImage = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 100) / 2, 100, 100, 100)];
    //把圖片設(shè)置成圓形。  我這里在故事版里面設(shè)置的imageView是一個正方形(因為頭像圖片都是放在正方形的imageView里)
    //裁成圓角
    self.changeImage.layer.cornerRadius = self.changeImage.frame.size.width/2;
    //隱藏裁剪掉的部分
    self.changeImage.layer.masksToBounds = YES;
    self.changeImage.layer.borderWidth = 2.0f;
    self.changeImage.layer.borderColor = [UIColor redColor].CGColor;
    self.changeImage.image = [UIImage imageNamed:@"timg.png"];
    [self.view addSubview:self.changeImage];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 140) / 2, 250, 140, 50)];
    label.text = @"點擊圖片更換圖像";
    [self.view addSubview:label];
    /**
     *  添加手勢:當點擊圖片的時候,彈出手勢對應(yīng)的事件
     *
     */
     //允許用戶交互
    _changeImage.userInteractionEnabled = YES;
    
    //初始化一個手勢
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self                                                                        action:@selector(alterHeadPortrait:)];
    //添加手勢
    [_changeImage addGestureRecognizer:singleTap];
}

效果如圖:


更換圖片之前.png

2.點擊事件的執(zhí)行,通過代理調(diào)用系統(tǒng)相機或者系統(tǒng)相冊

//點擊事件的執(zhí)行
- (void)alterHeadPortrait:(UIGestureRecognizer *)gesture{
    /**
     *通過UIAlertController添加彈出提示框
     */
    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //從相冊選擇,類型:UIAlertActionStyleDefault
    [alert  addAction:[UIAlertAction actionWithTitle:@"從相冊選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //初始化UIImagePickerController
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc] init];
        //獲取方式1, 通過相冊(呈現(xiàn)全部相冊), UIImagePickerControllerSourceTypePhotoLibrary
        //獲取方式2, 通過相機,UIImagePickerControllerSourceTypeCamera
        //獲取方法3, 通過相冊(呈現(xiàn)全部圖片),UIImagePickerControllerSourceTypeSavedPhotosAlbum
        PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //允許編輯,即放大裁剪
        PickerImage.allowsEditing = YES;
        //自代理
        PickerImage.delegate = self;
        //頁面跳轉(zhuǎn)
        [self presentViewController:PickerImage animated:YES completion:nil];
        
    }]];
    
    //拍照,類型:UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        /**
         其實和從相冊選擇一樣,只是獲取方式不同,前面是通過相冊,而現(xiàn)在,我們要通過相機的方式
         */
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
        //獲取方式:通過相機
        PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
        PickerImage.allowsEditing = YES;
        PickerImage.delegate = self;
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    
    //取消,類型:UIAlertActionStyleCancel
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
}

提示框效果如圖:


調(diào)出提示框.png

3.圖片更換成功后將更換后的圖片展示在UIImageView上

//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定義一個newPhoto,用來存放我們選擇的圖片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    _changeImage.image = newPhoto;
    [self dismissViewControllerAnimated:YES completion:nil];
   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]];   // 保存文件的名稱
    [UIImagePNGRepresentation(newPhoto)writeToFile: filePath    atomically:YES];
    
    //保存照片到本地
    [self dicPaths];
}

更換圖片之后效果:


更換圖片之后.png

二.保存到本地
1.將圖片寫入本地

//保存圖片信息到本地
-(void)dicPaths
{
    NSMutableArray *specialArr = [[NSMutableArray alloc] initWithCapacity:0];
    self.dic = [[NSMutableDictionary alloc]init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]];   // 保存文件的名稱
    NSLog(@"保存的路徑:%@", filePath);
    [self.dic setObject:filePath forKey:@"img"];
    [specialArr addObject:self.dic];
}

2.從本地讀取照片信息

//從本地獲取圖片 并賦值
-(void)plist
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]];   // 保存文件的名稱
    UIImage *img = [UIImage imageWithContentsOfFile:filePath];
    [_changeImage setImage:img];
}

三.保存到服務(wù)端
圖片的讀取首先是在緩存中去取(此demo均為本地圖片暫時不考慮,下篇將對網(wǎng)絡(luò)請求圖片進行述說),倘若沒有的話需要到本地磁盤中獲取,若本地磁盤任然沒有找到就從網(wǎng)絡(luò)上獲取,即保存到服務(wù)端.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容