用UIImagePickerController需要知道

UIImagePickerController通過leak工具測試的時(shí)候會有內(nèi)存泄漏,通過Google了一番說把它寫成一個(gè)單例的就可以解決內(nèi)存泄漏.

抱歉,我自己經(jīng)過測試時(shí)候告訴你,寫成單例也不行.謝謝,如果有請告訴我

接下來上使用的代碼,我是用來錄像

-(void)useTheSystemRecordViewController{

? ? UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];

? ? imagePicker.delegate = self;

? ? //判斷是否可以拍攝

? ? if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

? ? ? ? //判斷是否擁有拍攝權(quán)限

? ? ? ? AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

? ? ? ? if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){

? ? ? ? ? ? return;

? ? ? ? }


? ? ? ? //拍攝

? ? ? ? imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

? ? ? ? //錄制的類型 下面為視頻


? ? ? ? imagePicker.mediaTypes=@[(NSString*)kUTTypeMovie];

? ? ? ? imagePicker.videoQuality? = UIImagePickerControllerQualityTypeIFrame960x540;


? ? ? ? //錄制的時(shí)長

? ? ? ? imagePicker.videoMaximumDuration=20.0;


? ? ? ? //模態(tài)視圖的彈出效果

? ? ? ? imagePicker.modalPresentationStyle=UIModalPresentationOverFullScreen;

? ? ? ? [self presentViewController:imagePicker animated:YES completion:^{

? ? ? ? ? ? _segement.selectedSegmentIndex = 0;

? ? ? ? ? ? _isVideo = NO;

? ? ? ? }];

? ? }

#pragma mark -錄制完成

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


? ? NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];


? ? //返回的媒體類型是照片或者視頻

? ? if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

? ? ? ? //照片的處理

? ? ? ? //? ? ? ? [picker dismissViewControllerAnimated:YES completion:^{

? ? ? ? //? ? ? ? ? ? UIImage *img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

? ? ? ? //? ? ? ? }];


? ? }else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){


? ? ? ? //視頻的處理

? ? ? ? [picker dismissViewControllerAnimated:YES completion:^() {

? ? ? ? ? ? MBProgressHUD* progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

? ? ? ? ? ? progressHUD.removeFromSuperViewOnHide = YES;

? ? ? ? ? ? [progressHUD showAnimated:YES];

? ? ? ? ? ? _progressHUD = progressHUD;

? ? ? ? ? ? //文件管理器

? ? ? ? ? ? NSFileManager* fm = [NSFileManager defaultManager];


? ? ? ? ? ? NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myMovie.mp4"];

? ? ? ? ? ? NSURL *mergeFileURL = [NSURL fileURLWithPath:pathToMovie];


? ? ? ? ? ? NSFileManager* manager = [NSFileManager defaultManager];

? ? ? ? ? ? if ([manager fileExistsAtPath:pathToMovie]) {

? ? ? ? ? ? ? ? [manager removeItemAtPath:pathToMovie error:nil];

? ? ? ? ? ? }


? ? ? ? ? ? NSString* mp4Path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.mp4"];


? ? ? ? ? ? if ([manager fileExistsAtPath:mp4Path]) {

? ? ? ? ? ? ? ? [manager removeItemAtPath:mp4Path error:nil];

? ? ? ? ? ? }

? ? ? ? ? ? //通過文件管理器將視頻存放的創(chuàng)建的路徑中

? ? ? ? ? ? [fm copyItemAtURL:[info objectForKey:UIImagePickerControllerMediaURL] toURL:mergeFileURL error:nil];

? ? ? ? ? ? [self encodeVideoOrientation:mergeFileURL];


? ? ? ? ? ? /*

? ? ? ? ? ? AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:pathToMovie]];


? ? ? ? ? ? //根據(jù)AVURLAsset得出視頻的時(shí)長

? ? ? ? ? ? CMTime? time = [asset duration];

? ? ? ? ? ? int seconds = ceil(time.value/time.timescale);

? ? ? ? ? ? NSString *videoTime = [NSString stringWithFormat:@"%d",seconds];

? ? ? ? ? ? */

? ? ? ? ? ? //可以根據(jù)需求判斷是否需要將錄制的視頻保存到系統(tǒng)相冊中

? ? ? ? ? ? //? ? ? ? ? ? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

? ? ? ? ? ? //? ? ? ? ? ? NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];

? ? ? ? ? ? //

? ? ? ? ? ? //? ? ? ? ? ? if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) {

? ? ? ? ? ? //? ? ? ? ? ? ? ? [library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completionBlock:^(NSURL *assetURL, NSError *error){

? ? ? ? ? ? //

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];

? ? ? ? ? ? //? ? ? ? ? ? }


? ? ? ? }];

? ? }

}

-(void)encodeVideoOrientation:(NSURL*)anOutputFileURL{


? ? AVURLAsset * videoAsset = [[AVURLAsset alloc]initWithURL:anOutputFileURL options:nil];


? ? AVAssetExportSession * assetExport = [[AVAssetExportSession alloc] initWithAsset:videoAsset

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? presetName:AVAssetExportPresetMediumQuality];

? ? NSString* mp4Path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.mp4"];

? ? assetExport.outputURL = [NSURL fileURLWithPath: mp4Path];

? ? assetExport.shouldOptimizeForNetworkUse = YES;

? ? assetExport.outputFileType = AVFileTypeMPEG4;

? ? assetExport.videoComposition = [self getVideoComposition:videoAsset];

? ? [assetExport exportAsynchronouslyWithCompletionHandler:^{

? ? ? ? switch ([assetExport status]) {

? ? ? ? ? ? case AVAssetExportSessionStatusFailed:

? ? ? ? ? ? {

? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusFailed!");

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? }


? ? ? ? ? ? case AVAssetExportSessionStatusCancelled:

? ? ? ? ? ? ? ? NSLog(@"Export canceled");

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case AVAssetExportSessionStatusCompleted:

? ? ? ? ? ? ? ? NSLog(@"Successful!");

? ? ? ? ? ? {

? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ? ? [self recordFinished];

? ? ? ? ? ? ? ? });

? ? ? ? ? ? }


? ? ? ? ? ? ? ? break;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? }];

}

重點(diǎn)在接下來的代碼,橫屏錄制的時(shí)候需要調(diào)整方向

#pragma mark - 解決錄像保存角度問題

-(AVMutableVideoComposition *) getVideoComposition:(AVAsset *)asset

{

? ? AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

? ? AVMutableComposition *composition = [AVMutableComposition composition];

? ? AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];

? ? CGSize videoSize = videoTrack.naturalSize;

? ? BOOL isPortrait_ = [self isVideoPortrait:asset];

? ? if(isPortrait_) {

? ? ? ? NSLog(@"video is portrait ");

? ? ? ? videoSize = CGSizeMake(videoSize.height, videoSize.width);

? ? }

? ? composition.naturalSize? ? = videoSize;

? ? videoComposition.renderSize = videoSize;

? ? // videoComposition.renderSize = videoTrack.naturalSize; //

? ? videoComposition.frameDuration = CMTimeMakeWithSeconds( 1 / videoTrack.nominalFrameRate, 600);


? ? AVMutableCompositionTrack *compositionVideoTrack;

? ? compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

? ? [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:videoTrack atTime:kCMTimeZero error:nil];

? ? AVMutableVideoCompositionLayerInstruction *layerInst;

? ? layerInst = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

? ? [layerInst setTransform:videoTrack.preferredTransform atTime:kCMTimeZero];

? ? AVMutableVideoCompositionInstruction *inst = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

? ? inst.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

? ? inst.layerInstructions = [NSArray arrayWithObject:layerInst];

? ? videoComposition.instructions = [NSArray arrayWithObject:inst];

? ? return videoComposition;

}

-(BOOL) isVideoPortrait:(AVAsset *)asset

{

? ? BOOL isPortrait = FALSE;

? ? NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];

? ? if([tracks? ? count] > 0) {

? ? ? ? AVAssetTrack *videoTrack = [tracks objectAtIndex:0];


? ? ? ? CGAffineTransform t = videoTrack.preferredTransform;

? ? ? ? // Portrait

? ? ? ? if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0)

? ? ? ? {

? ? ? ? ? ? isPortrait = YES;

? ? ? ? }

? ? ? ? // PortraitUpsideDown

? ? ? ? if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0)? {


? ? ? ? ? ? isPortrait = YES;

? ? ? ? }

? ? ? ? // LandscapeRight

? ? ? ? if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0)

? ? ? ? {

? ? ? ? ? ? isPortrait = FALSE;

? ? ? ? }

? ? ? ? // LandscapeLeft

? ? ? ? if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0)

? ? ? ? {

? ? ? ? ? ? isPortrait = FALSE;

? ? ? ? }

? ? }

? ? return isPortrait;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末浮还,一起剝皮案震驚了整個(gè)濱河市驻龟,隨后出現(xiàn)的幾起案子忆首,更是在濱河造成了極大的恐慌,老刑警劉巖挟伙,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異模孩,居然都是意外死亡尖阔,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門榨咐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來介却,“玉大人,你說我怎么就攤上這事块茁〕菘溃” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長胃夏。 經(jīng)常有香客問我轴或,道長,這世上最難降的妖魔是什么仰禀? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任照雁,我火速辦了婚禮,結(jié)果婚禮上答恶,老公的妹妹穿的比我還像新娘饺蚊。我一直安慰自己,他們只是感情好悬嗓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布污呼。 她就那樣靜靜地躺著,像睡著了一般包竹。 火紅的嫁衣襯著肌膚如雪燕酷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天周瞎,我揣著相機(jī)與錄音苗缩,去河邊找鬼。 笑死声诸,一個(gè)胖子當(dāng)著我的面吹牛酱讶,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播彼乌,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼泻肯,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了慰照?” 一聲冷哼從身側(cè)響起灶挟,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎毒租,沒想到半個(gè)月后膏萧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蝌衔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年榛泛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片噩斟。...
    茶點(diǎn)故事閱讀 39,696評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡曹锨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出剃允,到底是詐尸還是另有隱情沛简,我是刑警寧澤齐鲤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站椒楣,受9級特大地震影響给郊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜捧灰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一扰藕、第九天 我趴在偏房一處隱蔽的房頂上張望区拳。 院中可真熱鬧,春花似錦、人聲如沸亦镶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽淑翼。三九已至,卻和暖如春阀溶,著一層夾襖步出監(jiān)牢的瞬間腻脏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工银锻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留永品,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓徒仓,卻偏偏與公主長得像腐碱,于是被迫代替她去往敵國和親誊垢。 傳聞我的和親對象是個(gè)殘疾皇子掉弛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容