溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS如何實(shí)現(xiàn)裁剪圓形圖像并顯示

發(fā)布時(shí)間:2021-12-24 15:55:31 來源:億速云 閱讀:288 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章給大家分享的是有關(guān)iOS如何實(shí)現(xiàn)裁剪圓形圖像并顯示的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

        本文主要是在iOS 10環(huán)境下使用,此時(shí)如果要使用使用系統(tǒng)照片庫、照相機(jī)等功能需要授權(quán),授權(quán)方法如下:

        右鍵點(diǎn)擊工程目錄中的“Info.plist文件——>Open As ——>Source Code”,打開復(fù)制以下你在應(yīng)用中使用的隱私權(quán)限設(shè)置(描述自己修改):

<key>NSVideoSubscriberAccountUsageDescription</key>
    <string></string>
    <key>NSBluetoothPeripheralUsageDescription</key>
    <string>藍(lán)牙權(quán)限</string>
    <key>NSSpeechRecognitionUsageDescription</key>
    <string>語音識別權(quán)限</string>
    <key>NSSiriUsageDescription</key>
    <string>Siri權(quán)限</string>
    <key>NSRemindersUsageDescription</key>
    <string></string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>相冊權(quán)限</string>
    <key>kTCCServiceMediaLibrary</key>
    <string></string>
    <key>NSMotionUsageDescription</key>
    <string>運(yùn)動(dòng)權(quán)限</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>麥克風(fēng)權(quán)限</string>
    <key>NSAppleMusicUsageDescription</key>
    <string>音樂權(quán)限</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>地理位置權(quán)限</string>
    <key>NSLocationUsageDescription</key>
    <string>地理位置權(quán)限</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>地理位置權(quán)限</string>
    <key>NSHomeKitUsageDescription</key>
    <string></string>
    <key>NSHealthUpdateUsageDescription</key>
    <string>健康權(quán)限</string>
    <key>NSHealthShareUsageDescription</key>
    <string>健康權(quán)限</string>
    <key>NSContactsUsageDescription</key>
    <string>通訊錄權(quán)限</string>
    <key>NSCameraUsageDescription</key>
    <string>攝像頭權(quán)限</string>
    <key>NSCalendarsUsageDescription</key>
    <string>日歷權(quán)限</string>

        下面,正式進(jìn)入本文要實(shí)現(xiàn)的功能的代碼編寫。

1. 使用Xcode的storyboard創(chuàng)建一個(gè)button和一個(gè)p_w_picpathView

創(chuàng)建后的效果如下圖1所示。其中,p_w_picpathView的尺寸影響最終顯示的效果尺寸,請根據(jù)實(shí)際情況設(shè)置。

iOS如何實(shí)現(xiàn)裁剪圓形圖像并顯示

2. 創(chuàng)建一個(gè)UIImage的類別(Category)

創(chuàng)建新文件,選擇“Objective-C File”,如下圖2所示:

iOS如何實(shí)現(xiàn)裁剪圓形圖像并顯示

在彈出的如圖3所示的對話框中,“File”寫入類別的名稱(本例中是DY),“File Type”選擇Category,“Class”選擇UIImage。然后點(diǎn)擊“Next”按鈕,將新文件保存。

iOS如何實(shí)現(xiàn)裁剪圓形圖像并顯示

3. 編寫類別中的代碼

UIImage+DY.h文件中

#import <UIKit/UIKit.h>
@interface UIImage (DY)
+ (instancetype)circleOldImage:(UIImage *)originalImage borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor;
@end

UIImage+DY.m文件中

#import "UIImage+DY.h"
@implementation UIImage (DY)
+ (instancetype)circleOldImage:(UIImage *)originalImage borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
{
    // 1.加載原圖
    UIImage *oldImage = originalImage;
    
    // 2.開啟上下文
    CGFloat p_w_picpathW = oldImage.size.width + 2 * borderWidth;
    CGFloat p_w_picpathH = oldImage.size.height + 2 * borderWidth;
    CGSize p_w_picpathSize = CGSizeMake(p_w_picpathW, p_w_picpathH);
    UIGraphicsBeginImageContextWithOptions(p_w_picpathSize, NO, 0.0);
    
    // 3.取得當(dāng)前的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 4.畫邊框(大圓)
    [borderColor set];
    CGFloat bigRadius = p_w_picpathW * 0.5; // 大圓半徑
    CGFloat centerX = bigRadius; // 圓心
    CGFloat centerY = bigRadius;
    CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
    CGContextFillPath(ctx); // 畫圓
    
    // 5.小圓
    CGFloat smallRadius = bigRadius - borderWidth;
    CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
    // 裁剪(后面畫的東西才會(huì)受裁剪的影響)
    CGContextClip(ctx);
    
    // 6.畫圖
    [oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)];
    
    // 7.取圖
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 8.結(jié)束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}
@end

 +(instancetype)circleOldImage:(UIImage *)originalImage borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor方法的說明:

 1. 這是一個(gè)類方法,最終返回的是一個(gè)UIImage的類;

 2. 方法中originalImage參數(shù)指的是從照片庫或者拍照后選中的照片(可能是經(jīng)過系統(tǒng)裁剪的);

 3. 方法中borderWidth參數(shù)指的是最終顯示的圓形圖像的邊框的寬度,可以可以根據(jù)自己的需要設(shè)置寬度;

 4. 方法中的borderColor參數(shù)指的是最終顯示的圓形圖像的邊框的顏色,可以可以根據(jù)自己的需要設(shè)置顏色。


4. 實(shí)現(xiàn)裁剪成圓形圖像并顯示

ViewController.h文件

#import <UIKit/UIKit.h>
#import "UIImage+DY.h"  //加載類別
@interface ViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>  //一定要添加這兩個(gè)Delegate
@property (strong, nonatomic) UIImagePickerController *p_w_picpathPickerController;
- (IBAction)btnPressed:(id)sender;
@property (strong, nonatomic) IBOutlet UIImageView *ablumImageView;
@end

ViewController.m文件

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btnPressed:(id)sender {
    
    if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
        
        //首先判斷是否支持照片庫,這個(gè)方法中的參數(shù)要和_p_w_picpathPickerController.sourceType的值保持一致
        
        //如果支持
        
        _p_w_picpathPickerController = [[UIImagePickerController alloc]init];
        
        _p_w_picpathPickerController.view.backgroundColor = [UIColor orangeColor];
        _p_w_picpathPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        _p_w_picpathPickerController.delegate = self;
        _p_w_picpathPickerController.allowsEditing = YES;  //該參數(shù)默認(rèn)是NO,建議設(shè)置為YES,否則裁剪成圓形圖片的方法將獲取到的是橢圓形的圖片,與你的預(yù)想大相徑庭
        
        [self presentViewController:_p_w_picpathPickerController animated:YES completion:nil];
        
    }
    
}
- (void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    _ablumImageView.p_w_picpath = [UIImage circleOldImage:[info objectForKey:UIImagePickerControllerEditedImage] borderWidth:30.0f borderColor:[UIColor orangeColor]];  
    //該方法中Info的Key值“UIImagePickerControllerEditedImage”表示的是選擇裁剪后的圖片,如果使用這個(gè)Key值,則_p_w_picpathPickerController.allowsEditing的值需要設(shè)置為YES。
    
    //如果_p_w_picpathPickerController.allowsEditing的值設(shè)置的NO,則這個(gè)Key的值應(yīng)該設(shè)置為UIImagePickerControllerOriginalImage
    
    /*
    info中的Key的值有如下幾個(gè):
    
    NSString *const  UIImagePickerControllerMediaType ;指定用戶選擇的媒體類型(文章最后進(jìn)行擴(kuò)展)
NSString *const  UIImagePickerControllerOriginalImage ;原始圖片
NSString *const  UIImagePickerControllerEditedImage ;修改后的圖片
NSString *const  UIImagePickerControllerCropRect ;裁剪尺寸
NSString *const  UIImagePickerControllerMediaURL ;媒體的URL
NSString *const  UIImagePickerControllerReferenceURL ;原件的URL
NSString *const  UIImagePickerControllerMediaMetadata;當(dāng)來數(shù)據(jù)來源是照相機(jī)的時(shí)候這個(gè)值才有效
    
    */
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)p_w_picpathPickerControllerDidCancel:(UIImagePickerController *)picker{
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
@end

感謝各位的閱讀!關(guān)于“iOS如何實(shí)現(xiàn)裁剪圓形圖像并顯示”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI