2015-09-21 7 views
6

मैं वीडियो से थंबनेल प्राप्त करने और इसे अपने टेबलव्यू में दिखाने की कोशिश कर रहा हूं। यहां मेरा कोड है:आईओएस - बिना वीडियो के थंबनेल कैसे प्राप्त करें?

- (UIImage *)imageFromVideoURL:(NSURL *)contentURL { 
    AVAsset *asset = [AVAsset assetWithURL:contentURL]; 

    // Get thumbnail at the very start of the video 
    CMTime thumbnailTime = [asset duration]; 
    thumbnailTime.value = 25; 

    // Get image from the video at the given time 
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 

    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL]; 
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    return thumbnail; 
} 

लेकिन छवि हमेशा काला लौटती है। क्या गलत है?

+1

टाइम.वल्यू मिलीसेकंड में है शायद समय पर फ्रेम काला है क्या आपने फ्रेम को किसी अन्य मूल्य –

+0

में आपकी मदद के लिए धन्यवाद दिया है! यह काम करता हैं। – TienLe

+0

अधिक स्वागत @TienLe –

उत्तर

1
NSURL *videoURL = [NSURL fileURLWithPath:filepath];// filepath is your video file path 



AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 
AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
NSError *error = NULL; 
CMTime time = CMTimeMake(1, 1); 
CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error]; 
NSLog(@"error==%@, Refimage==%@", error, refImg); 

UIImage *FrameImage= [[UIImage alloc] initWithCGImage:refImg]; 
return FrameImage; 
2

उपयोग करें: स्विफ्ट 3 -

func createThumbnailOfVideoFromFileURL(videoURL: String) -> UIImage? { 
    let asset = AVAsset(url: URL(string: videoURL)!) 
    let assetImgGenerate = AVAssetImageGenerator(asset: asset) 
    assetImgGenerate.appliesPreferredTrackTransform = true 
    let time = CMTimeMakeWithSeconds(Float64(1), 100) 
    do { 
     let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) 
     let thumbnail = UIImage(cgImage: img) 
     return thumbnail 
    } catch { 
     return UIImage(named: "ico_placeholder") 
    } 
} 

महत्वपूर्ण नोट:

के रूप में यह संसाधन व्यापक है तुम एक अगर किसी और में इस का उपयोग करने की आवश्यकता होगी। आपको छवि को सरणी या मॉडल में स्टोर करना होगा और जांचें कि यदि एक बार थंबनेल बनाया गया है तो यह कैश/सरणी को संदर्भित करता है ताकि cellForRowAtIndexPath आपके UITableView

-1

स्क्रॉल करने में कोई अंतराल नहीं पैदा करता है, तो कृपया " वीडियो से थंबनेल जनरेट "

https://littlebitesofcocoa.com/115-generating-thumbnails-from-videos

यह काफी एक ऐप्लिकेशन क्या एक वीडियो में है के एक या अधिक थंबनेल (छोटे अभी भी छवि पूर्वावलोकन) प्रदर्शित करने के लिए की जरूरत के लिए आम बात है। हालांकि, इस बात पर निर्भर करता है कि वीडियो कहां से आ रहा है, इसके लिए हमारे पास पूर्व-निर्मित थंबनेल (ओं) तक आसानी से पहुंच नहीं हो सकती है। आइए देखते हैं कि हम अपने आप को पकड़ने के लिए AVAssetImageGenerator का उपयोग कैसे कर सकते हैं। हम वीडियो के लिए एक साधारण NSURL से शुरू करते हैं, यह स्थानीय या दूरस्थ हो सकता है। हम इसके साथ एक एवीएसेट बनाएंगे और यह एक नई AVAssetImageGenerator ऑब्जेक्ट के लिए। हम जेनरेटर को पसंदीदा ट्रांसफॉर्म लागू करने के लिए कॉन्फ़िगर करेंगे ताकि हमारे थंबनेल सही अभिविन्यास में हों।

import AVFoundation 

if let asset = AVAsset(URL: videoURL) { 
    let durationSeconds = CMTimeGetSeconds(asset.duration) 
    let generator = AVAssetImageGenerator(asset: asset) 

    generator.appliesPreferredTrackTransform = true 

    let time = CMTimeMakeWithSeconds(durationSeconds/3.0, 600) 
    var thumbnailImage: CGImageRef 
    generator.generateCGImagesAsynchronouslyForTimes([NSValue(CMTime: time)]) { 
     (requestedTime: CMTime, thumbnail: CGImage?, actualTime: CMTime, result: AVAssetImageGeneratorResult, error: NSError?) in 
     self.videoThumbnailImageView.image = UIImage(CGImage: thumbnail) 
    } 
} 
+0

कृपया उस लिंक की गई सामग्री को अपने उत्तर में डालने पर विचार करें। – muttonUp

0

बस इस कोड का उपयोग करें .. अपना वीडियो यूआरएल पास करें और एक छवि प्राप्त करें।

+(UIImage *)getPlaceholderImageFromVideo:(NSString *)videoURL { 
    NSURL *url = [NSURL URLWithString:videoURL]; 
    AVAsset *asset = [AVAsset assetWithURL:url]; 
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
    CMTime time = [asset duration]; 
    time.value = 0; 
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; 
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return thumbnail; 
} 

आशा है, यह वही है जो आप खोज रहे हैं। कोई चिंता मेरे पास वापस आती है। :)

+0

इस फ़ंक्शन स्क्रॉल के कारण यह फ़ंक्शन कम समय नहीं ले रहा है। –

संबंधित मुद्दे

 संबंधित मुद्दे