分享图片剪裁

- (UIImage *)snapshotView {

UIGraphicsBeginImageContextWithOptions(CGSizeMake(FBScreenW, FBScreenH - FBNavigationBarH), NO, [UIScreen mainScreen].scale);

CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(currentContext, 0, 0);

[self.viewController.view.layer renderInContext:currentContext];

UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *snapshotView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FBScreenW, FBScreenH - FBNavigationBarH)];

UIEdgeInsets snapshotEdgeInsets = UIEdgeInsetsZero;

snapshotView.image = [snapshotImage resizableImageWithCapInsets:snapshotEdgeInsets];

return snapshotView.image;

}

- (void)composeImg {

UIImage *img = [UIImage imageNamed:@"image_forUse_image1"];

CGImageRef imgRef = img.CGImage;

CGFloat w = FBScreenW;

CGFloat h = w / 2;

//以1.png的图大小为底图

UIImage *img1 = [self snapshotView];

CGImageRef imgRef1 = img1.CGImage;

CGFloat w1 = FBScreenW;

CGFloat h1 = FBScreenH;

//以1.png的图大小为画布创建上下文

UIGraphicsBeginImageContext(CGSizeMake(w1, h1));

[img1 drawInRect:CGRectMake(0, 0, w1, h1 - FBNavigationBarH)];//先把1.png画到上下文中

[img drawInRect:CGRectMake(0, h1 - h, w, h)];//再把小图放在上下文中

UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片

UIGraphicsEndImageContext();//关闭上下文

NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

NSString *filePath = [path stringByAppendingPathComponent:@"01.jpg"];

[UIImagePNGRepresentation(resultImg) writeToFile:filePath atomically:YES];//保存图片到沙盒

NSLog(@"%@", FBDocument);

CGImageRelease(imgRef);

CGImageRelease(imgRef1);


}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容