最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解iOS 裁剪圓形圖像并顯示(類似于微信頭像)

 更新時(shí)間:2017年06月26日 09:12:53   作者:三創(chuàng)iOS和PHP開發(fā)  
本篇文章主要介紹了iOS 裁剪圓形圖像并顯示(類似于微信頭像),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文主要講解如何從照片庫(kù)選擇一張照片后將其裁剪成圓形頭像并顯示,類似于微信頭像那種模式。

本文的方法也適用于當(dāng)時(shí)拍照獲取的圖像,方法類似,所以不再贅述。

本文主要是在iOS 10環(huán)境下使用,此時(shí)如果要使用使用系統(tǒng)照片庫(kù)、照相機(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>語(yǔ)音識(shí)別權(quán)限</string>
  <key>NSSiriUsageDescription</key>
  <string>Siri權(quán)限</string>
  <key>NSRemindersUsageDescription</key>
  <string></string>
  <key>NSPhotoLibraryUsageDescription</key>
  <string>相冊(cè)權(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è)imageView

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


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

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

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


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 imageW = oldImage.size.width + 2 * borderWidth;
  CGFloat imageH = oldImage.size.height + 2 * borderWidth;
  CGSize imageSize = CGSizeMake(imageW, imageH);
  UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);

  // 3.取得當(dāng)前的上下文
  CGContextRef ctx = UIGraphicsGetCurrentContext();

  // 4.畫邊框(大圓)
  [borderColor set];
  CGFloat bigRadius = imageW * 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ù)指的是從照片庫(kù)或者拍照后選中的照片(可能是經(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 *imagePickerController;

- (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]) {

    //首先判斷是否支持照片庫(kù),這個(gè)方法中的參數(shù)要和_imagePickerController.sourceType的值保持一致

    //如果支持

    _imagePickerController = [[UIImagePickerController alloc]init];

    _imagePickerController.view.backgroundColor = [UIColor orangeColor];
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    _imagePickerController.delegate = self;
    _imagePickerController.allowsEditing = YES; //該參數(shù)默認(rèn)是NO,建議設(shè)置為YES,否則裁剪成圓形圖片的方法將獲取到的是橢圓形的圖片,與你的預(yù)想大相徑庭

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

  }


}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

  _ablumImageView.image = [UIImage circleOldImage:[info objectForKey:UIImagePickerControllerEditedImage] borderWidth:30.0f borderColor:[UIColor orangeColor]]; 
  //該方法中Info的Key值“UIImagePickerControllerEditedImage”表示的是選擇裁剪后的圖片,如果使用這個(gè)Key值,則_imagePickerController.allowsEditing的值需要設(shè)置為YES。

  //如果_imagePickerController.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)imagePickerControllerDidCancel:(UIImagePickerController *)picker{


  [self dismissViewControllerAnimated:YES completion:nil];

}

@end

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS中震動(dòng)反饋(UIFeedbackGenerator)與系統(tǒng)震動(dòng)詳解

    iOS中震動(dòng)反饋(UIFeedbackGenerator)與系統(tǒng)震動(dòng)詳解

    最近要做一個(gè)項(xiàng)目,需要持續(xù)響鈴并振動(dòng),所以就有了這篇文章,下面這篇文章主要給大家介紹了關(guān)于iOS中震動(dòng)反饋(UIFeedbackGenerator)與系統(tǒng)震動(dòng)的相關(guān)資料,需要的朋友可以參考下
    2018-08-08
  • iOS開發(fā)基礎(chǔ)之C語(yǔ)言

    iOS開發(fā)基礎(chǔ)之C語(yǔ)言

    現(xiàn)在越來越多的iOS開發(fā)興趣愛好者投入到了iOS培訓(xùn)中,有的是已經(jīng)在職的員工,有的是還在就讀的學(xué)生,還有一些是完全零基礎(chǔ)的同學(xué),那么對(duì)于他們來說就要一切從基礎(chǔ)開始學(xué),首先從C語(yǔ)言學(xué)起
    2015-11-11
  • Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解

    Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解

    這篇文章主要給大家介紹了關(guān)于Objective-C中實(shí)例所占內(nèi)存的大小的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS的音頻文件的格式轉(zhuǎn)換示例

    iOS的音頻文件的格式轉(zhuǎn)換示例

    這篇文章主要介紹了iOS的音頻文件的格式轉(zhuǎn)換示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • 解決蘋果ios用js的Date()出現(xiàn)NaN的問題

    解決蘋果ios用js的Date()出現(xiàn)NaN的問題

    下面小編就為大家分享一篇解決蘋果ios用js的Date()出現(xiàn)NaN的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • IOS實(shí)現(xiàn)微信朋友圈相冊(cè)評(píng)論界面的翻轉(zhuǎn)過渡動(dòng)畫

    IOS實(shí)現(xiàn)微信朋友圈相冊(cè)評(píng)論界面的翻轉(zhuǎn)過渡動(dòng)畫

    現(xiàn)在很多人幾乎每天都離不開微信,大家有沒有發(fā)現(xiàn)在點(diǎn)開微信相冊(cè)的時(shí)候,想要在相冊(cè)圖片界面跳轉(zhuǎn)查看點(diǎn)贊和評(píng)論時(shí),微信會(huì)采用界面翻轉(zhuǎn)的過渡動(dòng)畫來跳轉(zhuǎn)到評(píng)論界面,點(diǎn)擊完成又會(huì)翻轉(zhuǎn)回到圖片界面,這不同于一般的導(dǎo)航界面滑動(dòng)動(dòng)畫,于是學(xué)著做了一下,有需要一起看看。
    2016-08-08
  • 淺談iphone X的簡(jiǎn)單適配問題(推薦)

    淺談iphone X的簡(jiǎn)單適配問題(推薦)

    這篇文章主要介紹了淺談iphone X的簡(jiǎn)單適配(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • iOS自定義UIBarButtonItem的target和action示例代碼

    iOS自定義UIBarButtonItem的target和action示例代碼

    這篇文章主要給大家介紹了關(guān)于iOS自定義UIBarButtonItem的target和action的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • iOS中WKWebView仿微信加載進(jìn)度條

    iOS中WKWebView仿微信加載進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了iOS中WKWebView仿微信加載進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • iOS中 UIActionSheet字體的修改

    iOS中 UIActionSheet字體的修改

    這篇文章主要介紹了iOS中 UIActionSheet字體的修改,需要的朋友可以參考下
    2017-06-06

最新評(píng)論

高雄市| 绥化市| 新乡市| 星子县| 壶关县| 青川县| 莱西市| 阿坝县| 舒城县| 新兴县| 合水县| 营口市| 盐源县| 临泽县| 衡水市| 沂南县| 雷波县| 富民县| 周至县| 福贡县| 香港 | 九江县| 新化县| 左贡县| 古蔺县| 疏附县| 西盟| 五台县| 呼伦贝尔市| 铁力市| 景谷| 南投县| 泗阳县| 克东县| 乐陵市| 海城市| 乌拉特后旗| 盘山县| 白沙| 酒泉市| 济宁市|