UIImage: Resize, then Crop

后端 未结 16 2553
夕颜
夕颜 2020-11-22 11:51

I\'ve been bashing my face into this one for literally days now and even though I feel constantly that I am right on the edge of revelation, I simply cannot achieve my goal.

16条回答
  •  时光说笑
    2020-11-22 12:13

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0,0.0,ScreenWidth,ScreenHeigth)];
        [scrollView setBackgroundColor:[UIColor blackColor]];
        [scrollView setDelegate:self];
        [scrollView setShowsHorizontalScrollIndicator:NO];
        [scrollView setShowsVerticalScrollIndicator:NO];
        [scrollView setMaximumZoomScale:2.0];
        image=[image scaleToSize:CGSizeMake(ScreenWidth, ScreenHeigth)];
        imageView = [[UIImageView alloc] initWithImage:image];
        UIImageView* imageViewBk = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
        [self.view addSubview:imageViewBk];
        CGRect rect;
        rect.origin.x=0;
        rect.origin.y=0;
        rect.size.width = image.size.width;
        rect.size.height = image.size.height;
    
        [imageView setFrame:rect];
    
        [scrollView setContentSize:[imageView frame].size];
        [scrollView setMinimumZoomScale:[scrollView frame].size.width / [imageView frame].size.width];
        [scrollView setZoomScale:[scrollView minimumZoomScale]];
        [scrollView addSubview:imageView];
    
        [[self view] addSubview:scrollView];
    

    then you can take screen shots to your image by this

    float zoomScale = 1.0 / [scrollView zoomScale];
    CGRect rect;
    rect.origin.x = [scrollView contentOffset].x * zoomScale;
    rect.origin.y = [scrollView contentOffset].y * zoomScale;
    rect.size.width = [scrollView bounds].size.width * zoomScale;
    rect.size.height = [scrollView bounds].size.height * zoomScale;
    
    CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect);
    
    UIImage *cropped = [UIImage imageWithCGImage:cr];
    
    CGImageRelease(cr);
    

提交回复
热议问题