将缩略图保存到目录

时间:2014-03-26 07:34:11

标签: objective-c ios7

您好我要保存缩略图从视频到目录的持续时间和视频图标就像

enter image description here

如何可以像这样保存。 我使用代码将图像保存到目录,稍后我将在GridView中显示它。

if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
    {    

        // Getting Time Duration of video file

        NSURL* videoUrl =  alassetRep.url;
        AVURLAsset *avUrl = [AVURLAsset assetWithURL:videoUrl];
        CMTime time = [avUrl duration];

        NSUInteger dTotalSeconds = ceil(time.value/time.timescale);

        NSUInteger dMinutes = floor(dTotalSeconds % 3600 / 60);
        NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60);

        NSString *videoDurationText = [NSString stringWithFormat:@"%02i:%02i", dMinutes, dSeconds];
        NSLog(@"%@",videoDurationText);

        /************************************Low Resolution Images ******************************************/
        UIImage *image = [UIImage imageWithCGImage:[alassetRep fullResolutionImage]];
        UIImage *thumbImage = [self imageWithImage:image scaledToSize:CGSizeMake(50, 50)];

        NSData *thumbImageData = UIImageJPEGRepresentation(thumbImage, 0.8);

        NSString *thumbOriginalPath = [NSString stringWithFormat:@"SMALL_IMAGE_%d_%d.jpg",(int)currentDate,i];
        NSString* thumbImagePath = [DucPath stringByAppendingPathComponent:thumbOriginalPath];
        NSLog(@"Image path At Save Time:%@",thumbImagePath);
        [thumbImageData writeToFile:thumbImagePath atomically:YES];

        [pMediaArray addObject:thumbOriginalPath];
    }

1 个答案:

答案 0 :(得分:0)

是的,我找到了解决方案......

if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
    {


        unsigned long DataSize = (unsigned long)[alassetRep size];
        Byte *buffer = (Byte*)malloc(DataSize);
        NSUInteger buffered = (NSUInteger)[alassetRep getBytes:buffer fromOffset:0.0 length:(NSUInteger)alassetRep.size error:nil];
        NSData *videoData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

        NSString* newVideoName = [NSString stringWithFormat:@"video_%d_%d.mov",(int)currentDate,i];
        NSString* newVideoPath = [DucPath stringByAppendingPathComponent:newVideoName];
        [videoData writeToFile:newVideoPath atomically:YES];

        [pImageMediaArray addObject:newVideoName];
        NSLog(@"%@",newVideoName);


        // Getting Time Duration of video file

        NSURL* videoUrl =  alassetRep.url;
        AVURLAsset *avUrl = [AVURLAsset assetWithURL:videoUrl];
        CMTime time = [avUrl duration];

        NSUInteger dTotalSeconds = ceil(time.value/time.timescale);

        NSUInteger dMinutes = floor(dTotalSeconds % 3600 / 60);
        NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60);

        NSString *videoDurationText = [NSString stringWithFormat:@"%02i:%02i", dMinutes, dSeconds];
            /************************************Low Resolution Images ******************************************/
        UIImage *lowResImage = [UIImage imageWithCGImage:[alassetRep fullResolutionImage]];
        UIImage *thumbImage = [self imageWithImage:lowResImage scaledToSize:CGSizeMake(50, 50)];

        UIImageView* imageView = [[UIImageView alloc] initWithFrame:(CGRect){ CGPointZero, thumbImage.size}];
        imageView.image = thumbImage;
        UIImageView* timeView = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.frame.size.width - 35, imageView.frame.size.height - 20, 30, 15)];
        timeView.image = [UIImage imageNamed:@"screen2-videoTime-icon.png"];

        UIImageView* videoIcon = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.frame.origin.x+5, imageView.frame.size.height - 15, 15, 10)];
        videoIcon.image = [UIImage imageNamed:@"screen2-video-icon.png"];

        UILabel* timelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 15)];
        timelabel.textColor = [UIColor whiteColor];
        timelabel.text = videoDurationText;
        timelabel.textAlignment = NSTextAlignmentCenter;
        timelabel.font = [UIFont fontWithName:@"Helvetica" size: 10.0];

        [timeView addSubview:timelabel];
        [timeView bringSubviewToFront:timelabel];
        [imageView addSubview:videoIcon];
        [imageView addSubview:timeView];

        UIGraphicsBeginImageContext(imageView.frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [imageView.layer renderInContext:context];
        UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        NSData *thumbImageData = UIImageJPEGRepresentation(screenShot, 0.8);

        NSString *thumbOriginalPath = [NSString stringWithFormat:@"SMALL_IMAGE_%d_%d.jpg",(int)currentDate,i];
        NSString* thumbImagePath = [DucPath stringByAppendingPathComponent:thumbOriginalPath];
        NSLog(@"Image path At Save Time:%@",thumbImagePath);
        [thumbImageData writeToFile:thumbImagePath atomically:YES];

        [pMediaArray addObject:thumbOriginalPath];
    }