objective c - How to get the QLPreviewView Content and write to an image -


i write small demo. of feature use qlpreviewview give quick of pages file. when app runs, can scroll view pages file content, , when click save png button, app save current content displayed png image file. can implementation in save method. tried 2 implementation in method, neither of them worked. got blank image filled window background color. advise here? thanks.

the code , app screen shot can found here http://dr.ibuick.com/updu

#import <cocoa/cocoa.h> #import <quartz/quartz.h> #import <quicklook/quicklook.h> #import "ibappdelegate.h"  @interface ibappdelegate (qlpreviewitem) <qlpreviewitem>  @end  @implementation ibappdelegate (qlpreviewitem)      - (nsurl *)previewitemurl     {         return self.resolvedfileurl;     }      - (nsstring *)previewitemtitle     {         return [self.originalurl absolutestring];     }  @end  @implementation ibappdelegate      - (void)applicationdidfinishlaunching:(nsnotification *)anotification     {         _resolvedfileurl = [nsurl fileurlwithpath:@"/users/buick/desktop/1.pages"];         _originalurl = [nsurl fileurlwithpath:@"/users/buick/desktop/1.pages"];         _previewview = [[qlpreviewview alloc] initwithframe:nsmakerect(0, 50, 480, 360)                          style:qlpreviewviewstylenormal];         [_previewview setpreviewitem:self];         [self.window.contentview addsubview:_previewview];     }      - (ibaction)save:(id)sender {          // method 1          [_previewview lockfocus];         nsbitmapimagerep* rep = [_previewview                  bitmapimagerepforcachingdisplayinrect:_previewview.bounds];         [_previewview cachedisplayinrect:_previewview.bounds tobitmapimagerep:rep];         [_previewview unlockfocus];         [[rep representationusingtype:nspngfiletype properties:nil]              writetofile:@"/users/buick/desktop/1.png" atomically:yes];          // method 2         [_previewview lockfocus];          nsbitmapimagerep *bits;         bits = [[nsbitmapimagerep alloc]             initwithfocusedviewrect:[_previewview visiblerect]];         [_previewview unlockfocus];         nsdata *imagedata;         nsdictionary *imageprops = [nsdictionary dictionarywithobject:[nsnumber                          numberwithfloat:0.9] forkey:nsimagecompressionfactor];         imagedata = [bits representationusingtype:nsjpegfiletype                  properties:imageprops];         [imagedata writetofile:@"/users/buick/desktop/1.png" atomically:yes];     } @end 

ok think got it. try setting qlpreviewview type qlpreviewviewstylecompact when init it.

pv = [[qlpreviewview alloc] initwithframe:pv.bounds                                     style:qlpreviewviewstylecompact]; 

then use category nsimage+quicklook matt gemmel...

            nsimage *image = [nsimage imagewithpreviewoffileatpath:[[self.files objectatindex:i] path] ofsize:nsmakesize(1200, 1920) asicon:no]; 

Comments