UIGraphicsBeginImageContext(<specify your size here as CGRect>);
[<yourImageView or view you want to convert in image>.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
输出 viewImage . 如你想要一个黑色边框所以你可以有一个这样的逻辑,你在一个UIView上添加 imageView , blackBackroundColor 为黑色(请记住这个视图's frame should be greater than your imageView'的框架,每边有一些边距,这样你就可以拥有你的边框).... . 现在 [<yourView>.layer renderInContext:UIGraphicsGetCurrentContext()]; 使用这种方法 .
-(IBAction)saveViewToPhotoLibrary:(id)sender
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(screenImage,nil,nil,nil);
//fill cordinat in place of nil if u want only image comes and not button......ok thanks vijay//
UIGraphicsEndImageContext();
}
3 回答
输出
viewImage
. 如你想要一个黑色边框所以你可以有一个这样的逻辑,你在一个UIView上添加imageView
,blackBackroundColor
为黑色(请记住这个视图's frame should be greater than your imageView'的框架,每边有一些边距,这样你就可以拥有你的边框).... . 现在[<yourView>.layer renderInContext:UIGraphicsGetCurrentContext()];
使用这种方法 .希望这可以帮助 .