3

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];
}
David Berry
  • 40,941
  • 12
  • 84
  • 95
Tae Re
  • 163
  • 1
  • 8
  • It's not really clear what you mean by "scaling from the center" Unlike rotation, scaling is location insensitive. – David Berry May 09 '14 at 21:21
  • Hi david, sorry if unclear. I wanted the image to scale bigger with it's center in the middle of the screen. Right now it scales bigger but scales bigger from the top left of the screen. – Tae Re May 09 '14 at 22:37
  • Right now it looks like your transform is going to move the image up and left. What you want is to translate by delta width / 2, delta height / 2 (after scaling) – David Berry May 09 '14 at 22:43
  • Sorry for late response, but I was trying different things to get the delta or change to work properly. The only way it got close was adding an offset (int offset = scaleNumber * 100) to the mover. (CGAffineTransform Mover = CGAffineTransformMakeTranslation(deltaWidth/2 + offset, deltaHeight/2 + offset); I wish there was a property to get the asset to center to the view. Going to keep trying different methods, any other suggestions welcomed. thanks David. – Tae Re May 12 '14 at 07:11

1 Answers1

1

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 !

DimaC
  • 426
  • 2
  • 8
  • Thanks Rijii_Kot, never thought to use two CGAffineTransformConcat to get it to where I need it. How are you calculating the shrinkWidth and translateTocenter values? – Tae Re May 20 '14 at 02:22
  • finalTransform use translate and scale CGAffineTransform, i edit my answer. – DimaC May 21 '14 at 09:13
  • @Rijii_Kot Translation of your this line CGAffineTransform translate = CGAffineTransformMakeTranslation((x,y); in to swift is like this var translate = CGAffineTransform(translationX: (, y: x) – Khushbu Desai May 28 '18 at 07:13