【问题标题】:How do you convert a UIView into a UIImage for adding to a UIImageView? [duplicate]如何将 UIView 转换为 UIImage 以添加到 UIImageView? [复制]
【发布时间】:2014-03-18 19:34:39
【问题描述】:

我正在拍摄我的视图区域的快照并将其设置为新的 UIView。

UIView *currView = [self.view resizableSnapshotViewFromRect:mp.moviePlayer.view.frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
    [currView setFrame:CGRectMake(0, 0, 204, 128)];

然后我尝试使用此设置 UIIamgeView 图像:

[image2 setImage:[self getUIImageFromThisUIView:currView]];

-(UIImage*)getUIImageFromThisUIView:(UIView*)aUIView
{
    UIGraphicsBeginImageContext(aUIView.bounds.size);
    [aUIView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

但它没有将 UIImageView 的图像设置为任何东西? 提前致谢

【问题讨论】:

  • 这对于标准 UIKit 控件来说是正确的技术(尽管通常使用比例为 0 的 UIGraphicsBeginImageContextWithOptions 来捕获视网膜分辨率图像),但对于 MPMoviePlayerController,您应该使用 thumbnailImageAtTime。见stackoverflow.com/questions/13855340/…

标签: ios uiview uiimage cgcontext


【解决方案1】:

我使用类别来创建视图图像,

UIView+Snapshot.h
@interface UIView (Snapshot)

- (UIImage *) getSnapshot;
@end
UIView+Snapshot.m
#import "UIView+Snapshot.h"

@implementation UIView (Snapshot)

- (UIImage *) getSnapshot
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, self.window.screen.scale);

    [self drawViewHierarchyInRect:self.frame afterScreenUpdates:YES];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return snapshotImage;
}


@end

并添加表头,例如:

#import "UIView+Snapshot.h"

UIImage *image = [self.view getSnapshot];

【讨论】:

  • 这是一个不错的小类别(虽然,使用 iOS 7 drawViewHierarchyInRect 方法,所以它不适用于早期的 iO​​S 版本),但我不确定这是否能解决唯一的问题到MPMoviePlayerController,这会破坏使用标准UIKit 控件拍摄快照的典型尝试。
猜你喜欢
  • 2013-05-10
  • 2011-07-25
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 2012-12-25
相关资源
最近更新 更多