ioscgaffinetransformavmutablecompositionavassetcgaffinetransformscale

CGAffineTransform scale not scaling from center - with export


I guess what makes my question/problem different than other postings is that I am not scaling a view, but rather an asset layer instruction (AVMutableVideoCompositionLayerInstruction). So setting anchor points, view.center, CG Rect scaling all do not work.

Beyond moving the asset with CGAffineTransformMakeTranslation to get it to look like it was centered, but is highly inaccurate, I can not figure out how to make it scale from the center. Is there a property I'm missing? Docs and guides aren't very helpful, but maybe I missed something.

Code is below. Thank you all in advance!!! :)

Also, for those looking for way to export avasset with CGTransforms, the below code is all the steps to get there; of course need to fill out details like the CMTimeRanges, but hope this helps someone figure this confusing thing out.

-(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];
}

Solution

  • CGAffineTransform work in Quartz coordinate system, that's the point from where you should start:

    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];
    

    that kind of transform work for me.

    EDIT: my mistake, forget to edit all code !