CGAffineTransform缩放不从中心缩放 - 使用导出

时间:2014-05-09 19:15:47

标签: ios cgaffinetransform avmutablecomposition avasset cgaffinetransformscale

我想是什么让我的问题/问题与其他帖子不同的是我没有缩放视图,而是资产层指令(AVMutableVideoCompositionLayerInstruction)。因此设置锚点,view.center,CG Rect缩放都不起作用。

除了使用CGAffineTransformMakeTranslation移动资产以使其看起来像是居中,但是非常不准确之外,我无法弄清楚如何使其从中心扩展。有遗产吗?文档和指南不是很有帮助,但也许我错过了一些东西。

代码如下。谢谢大家!!! :)

此外,对于那些寻找使用CGTransforms导出avasset的方法,下面的代码是到达那里的所有步骤;当然需要填写像CMTimeRanges这样的细节,但希望这可以帮助某人解决这个令人困惑的事情。

-(void) goAssetsExport {
    AVMutableComposition *composition = [[AVMutableComposition alloc] init];

    AVMutableCompositionTrack *firstTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.frameDuration = CMTimeMake(1,30);
    videoComposition.renderScale = 1.0;
    videoComposition.renderSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);

    NSURL *movieURL = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"%@",   [preloadEffectsArray objectAtIndex:i]]  withExtension:@"mov"];
    AVURLAsset *firstAsset = [AVURLAsset URLAssetWithURL:movieURL options:nil];
    AVAssetTrack *firstAssetTrack = [[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    [firstTrack insertTimeRange:CMTimeRangeMake(firstTrackRangeMin, duration) ofTrack:firstAssetTrack atTime:firstTrackRangeMin error:nil];

    AVMutableVideoCompositionInstruction *transitionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

    AVMutableVideoCompositionLayerInstruction *fromLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack];


    //**This is where problem might be?**//
    CGAffineTransform Scaler = CGAffineTransformMakeScale(scaleNumber,scaleNumber);
    CGAffineTransform Mover = CGAffineTransformMakeTranslation(scaleNumber * -100, scaleNumber * -150);

    [fromLayer setTransform:CGAffineTransformConcat(Scaler,Mover) atTime:firstTrackRangeMin];

    transitionInstruction.layerInstructions = [NSArray arrayWithObject:fromLayer];

    videoComposition.instructions = instructionArray;

    [self exportVideo:composition withInstructionComposition:videoComposition];
}

1 个答案:

答案 0 :(得分:1)

CGAffineTransform在Quartz坐标系中工作,这是你应该从哪里开始的:

CGAffineTransform  translate = CGAffineTransformMakeTranslation((x,y);
CGAffineTransform  scale = CGAffineTransformMakeScale(q,z);
CGAffineTransform finalTransform = CGAffineTransformConcat(scale, CGAffineTransformConcat(translate, assetTrackForVideo1.preferredTransform));
        [layerInstructionForVideo setTransform:finalTransform atTime:kCMTimeZero];
        [layerInstructions addObject:layerInstructionForVideo];

这种转变对我有用。

编辑:我的错误,忘了编辑所有代码!