为什么这个GLView屏幕截图代码返回一个空白/黑色的UIImage?(Why is this GLView Screenshot code returning a blank/black UIImage?)
我正在使用以下代码截取
GLView
中的像素。 问题是,它返回一个完全黑色的UIImage
。 这个代码在LineDrawer.m
中被调用,它是GLView
代码的核心 - 所以它被从右边的.m文件中调用。 我怎样才能保存实际的截图而不是黑色的图像?- (UIImage*) getGLScreenshot { NSLog(@"1"); float scale = 0.0; if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { // scale value should be 1.0 on 3G and 3GS, and 2.0 on iPhone 4. scale = [[UIScreen mainScreen] scale]; } // these are swapped since the screen is rotatey float h = 768 * scale; float w = 924 * scale; NSInteger myDataLength = w * h * 4; // allocate array and read pixels into it. GLubyte *buffer = (GLubyte *) malloc(myDataLength); glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer); // gl renders "upside down" so swap top to bottom into new array. // there's gotta be a better way, but this works. GLubyte *buffer2 = (GLubyte *) malloc(myDataLength); for(int y = 0; y <h; y++) { for(int x = 0; x <w * 4; x++) { buffer2[(((int)h-1) - y) * (int)w * 4 + x] = buffer[y * 4 * (int)w + x]; } } // make data provider with data. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); // prep the ingredients int bitsPerComponent = 8; int bitsPerPixel = 32; int bytesPerRow = 4 * w; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; // make the cgimage CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); // then make the uiimage from that UIImage *myImage = [UIImage imageWithCGImage:imageRef]; return myImage; } - (void)saveGLScreenshotToPhotosAlbum { UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil); }
I am using the following code to take a screenshot of the pixels in a
GLView
. The problem is, it returns a completely blackUIImage
. This code is being called inLineDrawer.m
which is the heart of theGLView
code - so it is being called from the right .m file. How can I save the actual screenshot and not a black image?- (UIImage*) getGLScreenshot { NSLog(@"1"); float scale = 0.0; if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { // scale value should be 1.0 on 3G and 3GS, and 2.0 on iPhone 4. scale = [[UIScreen mainScreen] scale]; } // these are swapped since the screen is rotatey float h = 768 * scale; float w = 924 * scale; NSInteger myDataLength = w * h * 4; // allocate array and read pixels into it. GLubyte *buffer = (GLubyte *) malloc(myDataLength); glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer); // gl renders "upside down" so swap top to bottom into new array. // there's gotta be a better way, but this works. GLubyte *buffer2 = (GLubyte *) malloc(myDataLength); for(int y = 0; y <h; y++) { for(int x = 0; x <w * 4; x++) { buffer2[(((int)h-1) - y) * (int)w * 4 + x] = buffer[y * 4 * (int)w + x]; } } // make data provider with data. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); // prep the ingredients int bitsPerComponent = 8; int bitsPerPixel = 32; int bytesPerRow = 4 * w; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; // make the cgimage CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); // then make the uiimage from that UIImage *myImage = [UIImage imageWithCGImage:imageRef]; return myImage; } - (void)saveGLScreenshotToPhotosAlbum { UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil); }
原文:https://stackoverflow.com/questions/11168653
最满意答案
首先构建重复部分:
index = pd.RangeIndex(len(aa) + len(bb) + len(cc)) df = pd.DataFrame({'aa':aa[0], 'bb':bb[0], 'cc':cc[0]}, index)
这给你15份副本:
aa1 bb1 cc1
然后覆盖不同的部分:
df.aa[:len(aa)] = aa df.bb[len(aa):len(aa)+len(bb)] = bb df.cc[len(aa)+len(bb):] = cc
这给出了所需的输出。
First construct the repeating parts:
index = pd.RangeIndex(len(aa) + len(bb) + len(cc)) df = pd.DataFrame({'aa':aa[0], 'bb':bb[0], 'cc':cc[0]}, index)
That gives you 15 copies of:
aa1 bb1 cc1
Then overwrite the varying parts:
df.aa[:len(aa)] = aa df.bb[len(aa):len(aa)+len(bb)] = bb df.cc[len(aa)+len(bb):] = cc
Which gives the desired output.
相关问答
更多-
pandas dataframe group with with permutations(pandas dataframe group by with permutations)[2023-05-26]
根据您的要求,您的代码实际上是错误地使用permutations 。 您需要单独根据beta列进行置换,并使itertools.permutations取2个元素。 示例 - from itertools import permutations grouped = df.groupby('alpha') resultlist = [] for key,group in grouped: for b,g in permutations(group['beta'].tolist(),2): ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如果你传递一个列表(行),这将起作用: In [11]: pd.DataFrame(data) Out[11]: DC? building occupants 0 True White House Barack 1 True White House Michelle 2 True White House Sasha 3 True White House Malia In [12]: pd.DataFrame([data]) Out[12]: ...
-
您可以创建新系列 In [11]: df['D'] = pd.Series([[[4,-0.05],[0.03,[-0.02,0.02]],[0.01,-0.03]], [[4,-0.35],[0.07,[-0.02,0.02]],[0.91,-0.03]]]) In [12]: df Out[12]: A B C D 0 0 1 2 ...
-
首先构建重复部分: index = pd.RangeIndex(len(aa) + len(bb) + len(cc)) df = pd.DataFrame({'aa':aa[0], 'bb':bb[0], 'cc':cc[0]}, index) 这给你15份副本: aa1 bb1 cc1 然后覆盖不同的部分: df.aa[:len(aa)] = aa df.bb[len(aa):len(aa)+len(bb)] = bb df.cc[len(aa)+len(bb):] = cc 这给出了所需 ...
-
使用python和pandas创建组合而不是排列(Using python and pandas to create combinations instead of permutations)[2023-08-22]
您可以利用itertools的组合为每个用户创建独特的水果组合。 from itertools import combinations def func(group): return pd.DataFrame(list(combinations(group.fruit, 2)), columns=['fruit_x', 'fruit_y']) df.groupby('user').apply(func).reset_index(level=1, drop=True) fruit_x ... -
您希望为每个组获取索引。 它存储在groupbydataframe的'groups'属性中。 #filter for coverage==True #group by 'name' #access the 'groups' attribute by_person = df[df.coverage].groupby('name').groups 将返回: {'Jason': Int64Index([0, 5], dtype='int64'), 'Tina': Int64Index([4], dtype=' ...
-
从列表创建Pandas dataFrame,同时保留数据类型(create Pandas dataFrame from lists while preserving datatypes)[2024-04-08]
您可以从字典构造数据框,其中值是列表,键是标签/列: df = pd.DataFrame({'countries': countries, 'gold': gold, 'silver': silver, 'bronze':bronze}) df.dtypes #bronze int64 #countries object #gold int64 #silver int64 #dtype: object You can construct the da ... -
所有可能的排列列Pandas Dataframe在同一列中(All possible permutations columns Pandas Dataframe within the same column)[2023-08-22]
我试图将尽可能多的步骤链接在一起。 分解它们以查看每个步骤的作用:) df2 = pd.DataFrame(index=pd.MultiIndex.from_product([subdf['x'] for p, subdf in df.groupby('planid')], names=df.planid.unique())).reset_index().stack().reset_index() df2.columns = ['permutation_counter', 'planid', 'x'] d ... -
我认为您可以使用stack创建Series ,然后通过astype将list为string通过astype删除[]并最后调用get_dummies : df = df.stack().astype(str).str.strip('[]').str.get_dummies(sep=', ') print (df) 1 10 11 12 2 3 4 5 6 7 8 9 0 col1 1 0 0 0 1 1 0 0 0 0 0 0 col2 ...