How can I save the image from IKImageView? - objective-c

Okay... I want to save the visible rectangle of the IKImageView image only.
My problem is that, somehow, if I had an image in portrait mode it will be not saved in the right orientation. I drawing the image with this code:
[sourceImg drawInRect:targetRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];
On the other side if I draw the image with this code:
[sourceImg drawAtPoint:NSZeroPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];
It will be in the right orientation, but edits like zooming are lost. I mean it will save the right cut out, but unfortunately not in the right size.
Here is the code how I load the image:
- (IBAction)imageSelectionButtonAction:(id)sender {
NSLog(#"%s", __FUNCTION__);
NSOpenPanel *panel = [NSOpenPanel openPanel];
id model = [self getModel];
if (imageView) {
[panel setAllowedFileTypes:[NSImage imageFileTypes]];
[panel beginSheetModalForWindow:[[NSApplication sharedApplication] mainWindow] completionHandler:^(NSInteger returnCode) {
if (returnCode == 1) {
NSURL *imageUrl = [panel URL];
CGImageRef image = NULL;
CGImageSourceRef isr = CGImageSourceCreateWithURL( (__bridge CFURLRef)imageUrl, NULL);
if (isr) {
NSDictionary *options = [NSDictionary dictionaryWithObject: (id)kCFBooleanTrue forKey: (id) kCGImageSourceShouldCache];
image = CGImageSourceCreateImageAtIndex(isr, 0, (__bridge CFDictionaryRef)options);
if (image) {
_imageProperties = (__bridge_transfer NSDictionary*)CGImageSourceCopyPropertiesAtIndex(isr, 0, (__bridge CFDictionaryRef)_imageProperties);
_imageUTType = (__bridge NSString*)CGImageSourceGetType(isr);
}
CFRelease(isr);
}
if (image) {
[imageView setImage:image imageProperties:_imageProperties];
CGImageRelease(image);
}
[[model saveTempMutabDict] setValue:[imageUrl absoluteString] forKey:#"tempImage"];
}
}];
return;
}
}
And here is the code how I save it:
- (void)saveImage:(NSString *)path {
// get the current image from the image view
CGImageRef sourceImgRef = [imageView image];
NSRect targetRect = [imageView visibleRect];
NSImage *sourceImg = [[NSImage alloc] initWithCGImage:sourceImgRef size:NSZeroSize];
NSMutableDictionary *thisTiffDict = [_imageProperties valueForKey:#"{TIFF}"];
NSInteger theOrientation = [[thisTiffDict valueForKey:#"Orientation"] integerValue];
NSImage *targetImg = nil;
if (theOrientation == 6) {
targetImg = [[NSImage alloc] initWithSize:NSMakeSize([imageView frame].size.height, [imageView frame].size.width)];
} else {
targetImg = [[NSImage alloc] initWithSize:NSMakeSize([imageView frame].size.width, [imageView frame].size.height)];
}
NSRect sourceRect = [imageView convertViewRectToImageRect:targetRect];
[targetImg lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[sourceImg drawAtPoint:NSZeroPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];
[targetImg unlockFocus];
_saveOptions = [[IKSaveOptions alloc] initWithImageProperties:_imageProperties imageUTType: _imageUTType];
NSString * newUTType = [_saveOptions imageUTType];
CGImageRef targetImgRef = [targetImg CGImageForProposedRect:NULL context:[NSGraphicsContext currentContext] hints:nil];
if (targetImgRef) {
NSURL *url = [NSURL fileURLWithPath: path];
CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, (__bridge CFStringRef)newUTType, 1, NULL);
if (dest) {
CGImageDestinationAddImage(dest, targetImgRef, (__bridge CFDictionaryRef)[_saveOptions imageProperties]);
CGImageDestinationFinalize(dest);
CFRelease(dest);
}
}
}
I have no idea what I'm doing wrong?
Thank you
Jens

Related

Is there any way to Convert NSView Contain Subviews into NSImage?

Is there any way to Convert NSView Contain many Subviews with background color into NSImage ?
Sample code will be great
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[_collageView bounds]];
[_collageView unlockFocus];
// NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];
NSData *exportedData = [rep representationUsingType:NSPNGFileType properties:nil];
NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = #"Save chart";
[savepanel setAllowedFileTypes:[NSArray arrayWithObject:#"png"]];
[savepanel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
{
if (NSFileHandlingPanelOKButton == result)
{
NSURL* fileURL = [savepanel URL];
if ([fileURL.pathExtension isEqualToString:#""])
fileURL = [fileURL URLByAppendingPathExtension:#"png"];
[exportedData writeToURL:fileURL atomically:YES];
}
}];
i also used the following code to
[_collageView lockFocus];
NSBitmapImageRep *bits = [[NSBitmapImageRep alloc]initWithFocusedViewRect: [_collageView bounds]];
[_collageView unlockFocus];
NSImage *image1 = [[NSImage alloc] init];
[image1 addRepresentation: bits];
[self.ImageView setImage:image1];
but not working
Your approach copies the bitmap from the windows server. This is the rendered content of the view. Typically you do not want to do that.
It is a better way to lock the focus on an NSImage, which builds up the graphic contexts and then draw into that image using -drawRect: or whatever drawing methods you have. You can add subviews by simply iterating them.
// perhaps in a category of NSView
- (void)drawRecursively
{
[self drawRect:self.bounds];
for (NSView *subview in self.subviews)
{
[subview drawRecursively];
}
}
To start that
NSImage *image = [[NSImage alloc] initWithSize:theView.bounds.size];
[image lockFocus]; // Set-up the context for the image
[theView drawRecursively];
[image unlockFocus];
Typped in Safari.
Edit: Before going to the subviews, you have to perform a transformation for them. Forgot that.
I have actually implemented the drawing of a view hierarchy offscreen myself.
It is non-trivial, even for a very simple view hierarchy where all views are
subclasses NSControl and have -(BOOL) isFlipped { return YES; }
Here's one small portion of that code, which will give you an idea of the pain of doing this:
- (void)drawRect:(NSRect)iDirtyBigRect
{
#ifdef DISABLE_PSEUDOVIEW
debugRectsBeingDrawn();
return;
#endif
if ( !drawableShape )
{
if ( [[[self window] contentView] wantsLayer] )
{
CGRect cgRect = NSRectToCGRect([self bounds]);
drawableShape = HIShapeCreateMutableWithRect( &cgRect );
}
else
{
DUMP( #"our superview should have set up our drawableShape!" );
[self setNeedsDisplay:YES];
return;
}
}
else if ( iDirtyBigRect.size.width <= 0 )
{
// Our drawable shape is empty.
// This can happen for several valid reasons:
// 1) dirtyShape is fully masked by opaqueShape
// 2) for some reason, we were called to obey needsDisplay when not needed
DLog( #"nothing to draw %#", self );
return; // must return, otherwise we risk drawing outside our bounds
}
//NSArray *hardSubviews = [self subviews];
for ( NSView *pseudoSubview in pseudoSubviews )
{
if ( ![pseudoSubview isHidden] &&
[pseudoSubview window] == offscreenWindow ) // only draw pseudo view if it's in offscreen window
{
NSRect rectToDraw = [pseudoSubview frame];
// if ( rectToDraw.size.width == 0 )
// {
// DLog( #"clipped to superview %#", pseudoSubview );
// }
CGRect cgRect = NSRectToCGRect(rectToDraw);
if ( HIShapeIntersectsRect(drawableShape, &cgRect) )
{
// the magic: transform to subview coordinates
NSAffineTransform* xform = [NSAffineTransform transform];
[xform translateXBy:rectToDraw.origin.x yBy:rectToDraw.origin.y];
if ( [pseudoSubview isFlipped] != [self isFlipped] )
{
if ( ![pseudoSubview isKindOfClass: [NSColorWell class]] )
{
DUMP( #"hmmm flippedness different %d %d", [pseudoSubview isFlipped], [self isFlipped] );
}
[xform translateXBy:0.0 yBy:rectToDraw.size.height];
[xform scaleXBy:1.0 yBy:-1.0];
}
[xform concat];
HIMutableShapeRef newShape = HIShapeCreateMutableWithRect( &cgRect );
HIShapeIntersect( newShape, drawableShape, newShape ); // clip to subview frame
HIShapeOffset( newShape, -cgRect.origin.x, -cgRect.origin.y ); // translate to subview coords
CGRect dirtyRect;
HIShapeGetBounds(newShape, &dirtyRect);
// if ( dirtyRect.size.width <= 0 )
// {
// DUMP( #"why?" );
// }
if ( [pseudoSubview isKindOfClass:[PseudoView class]] )
{
//DLog( #"drawing Pseudo %# dirtyRect %# ", pseudoSubview, NSStringFromRect(NSRectFromCGRect(dirtyRect)));
PseudoView *pView = (PseudoView *)pseudoSubview;
if ( pView->drawableShape )
{
CFRelease( pView->drawableShape );
pView->drawableShape = NULL;
}
// create subview personal drawableShape from my drawable shape
pView->drawableShape = newShape;
if ( [pseudoSubview isOpaque] )
gDrawDirtyPixels += dirtyRect.size.width * dirtyRect.size.height;
if ( dirtyRect.size.width > 0 )
[pseudoSubview drawRect: NSRectFromCGRect(dirtyRect)];
}
else
{
//DLog( #"drawing non-Pseudo %# rectToDraw %# ", pseudoSubview, NSStringFromRect( rectToDraw ));
UInt64 t1 = CpuCycles();
CFRelease( newShape );
// sacrifice efficiency to avoid bugs...
// always draw the entire view.
[pseudoSubview drawRect:[pseudoSubview bounds]]; // NSRectFromCGRect(dirtyRect)]; // [pseudoSubview bounds]];
UnitTestQuartz( [pseudoSubview bounds] );
// NSLog( #"drawRect %# %# sUnitTestQuartzToggle %f\n -- superv %#\n -- self %#", pseudoSubview, NSStringFromRect(rectToDraw), sUnitTestQuartzToggle*100, [pseudoSubview superview], self );
UInt64 t2 = CpuCycles();
int diff = (int)(t2-t1);
gDrawControl += diff;
}
[xform invert]; // restore
[xform concat];
}
}
}
//if ( [self isKindOfClass:[PseudoRootView class]] )
// [offscreenWindow setViewsNeedDisplay:NO];
}
I am able to solve my problem with the following Code
// Setup the image to render
NSRect imgRect = _collageView.frame;
NSSize imgSize = imgRect.size;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:imgSize.width
pixelsHigh:imgSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0];
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:context];
// Render
CGContextRef zCgContextRef = (CGContextRef) [context graphicsPort];
[[_collageView layer] renderInContext:zCgContextRef];
// Get the Data for the image
NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];
// Start the savepanel
NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = #"Save chart";
[savepanel setAllowedFileTypes:[NSArray arrayWithObject:#"jpg"]];
[savepanel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
{
if (NSFileHandlingPanelOKButton == result)
{
NSURL* fileURL = [savepanel URL];
if ([fileURL.pathExtension isEqualToString:#""])
fileURL = [fileURL URLByAppendingPathExtension:#"jpg"];
[exportedData writeToURL:fileURL atomically:YES];
}
}];

Exporting unshown NSViewController to JPG

I am working on an desktop app that generates an image from en NSViewController that is not shown.
The view generated has 3 NSImageViews which change the content once created.
I manage to export the NSViewController's view to JPG file, but it never renders in the image given to the NSImageView, it keeps the image which is set in the nib. Also, if I don't set the background color to the view's layer... I get an empty result.
What am I missing here? I guess I need to update the NSViewController's view... but how can I do that? I do not want to show my NSViewController.
My code:
- (IBAction)doExport:(id)sender {
ExportView *view = [[ExportView alloc] initWithNibName:#"ExportView" bundle:nil];
[view.pic1 setImage:self.pic1.image];
[view.pic2 setImage:self.pic2.image];
[view.pic3 setImage:self.pic3.image];
[view.icon setImage:self.icon.image];
// Hardcoding for test
[view.pic1 setImage:[NSImage imageNamed:#"iPhone 4-Inch Screenshot 1"]];
[view.pic1 setNeedsLayout:YES];
[view.pic1 setNeedsDisplay];
[view.pic1 updateLayer];
// Setup the image to render
NSRect imgRect = view.view.frame;
NSSize imgSize = imgRect.size;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:imgSize.width
pixelsHigh:imgSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0];
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
// Set view background color
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:[NSColor clearColor].CGColor];
[view.view setWantsLayer:YES];
[view.view setLayer:viewLayer];
// Render
CGContextRef zCgContextRef = (CGContextRef) [g graphicsPort];
[[view.view layer] renderInContext:zCgContextRef];
// Render pic as well, for test
[view.pic1.layer renderInContext:zCgContextRef];
// Get the Data for the image
NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];
// Start the savepanel
NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = #"Save chart";
[savepanel setAllowedFileTypes:[NSArray arrayWithObject:#"jpg"]];
[savepanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result)
{
if (NSFileHandlingPanelOKButton == result)
{
NSURL* fileURL = [savepanel URL];
if ([fileURL.pathExtension isEqualToString:#""])
fileURL = [fileURL URLByAppendingPathExtension:#"jpg"];
[exportedData writeToURL:fileURL atomically:YES];
}
}];
}
I solved it in a very ugly way. Basically what I do is I add the view to my contentView and set setHidden to YES straight away. This will intialize the view. Now I change the content of my Nib view.
When its time to render the data to NSData I change setHidden to NO, generate the NSData and then remove the view.
The end result (no beautiful code :( ):
- (IBAction)doExport:(id)sender {
ExportView *view = [[ExportView alloc] initWithNibName:#"ExportView" bundle:nil];
[view.view setHidden:YES];
[self.window.contentView addSubview:view.view];
[view.pic1 setImage:self.pic1.image];
[view.pic2 setImage:self.pic2.image];
[view.pic3 setImage:self.pic3.image];
[view.icon setImage:self.icon.image];
// Setup the image to render
NSRect imgRect = view.view.frame;
NSSize imgSize = imgRect.size;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:imgSize.width
pixelsHigh:imgSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0];
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
// Set view background color
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:[NSColor clearColor].CGColor];
[view.view setWantsLayer:YES];
[view.view setLayer:viewLayer];
[view.view setHidden:NO];
// Render
CGContextRef zCgContextRef = (CGContextRef) [g graphicsPort];
[[view.view layer] renderInContext:zCgContextRef];
[view.view removeFromSuperview];
// Get the Data for the image
NSData *exportedData = [rep representationUsingType:NSJPEGFileType properties:nil];
// Start the savepanel
NSSavePanel *savepanel = [NSSavePanel savePanel];
savepanel.title = #"Save chart";
[savepanel setAllowedFileTypes:[NSArray arrayWithObject:#"jpg"]];
[savepanel setNameFieldStringValue:#"Test"];
[savepanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result)
{
if (NSFileHandlingPanelOKButton == result)
{
NSURL* fileURL = [savepanel URL];
if ([fileURL.pathExtension isEqualToString:#""])
fileURL = [fileURL URLByAppendingPathExtension:#"jpg"];
[exportedData writeToURL:fileURL atomically:YES];
}
}];
}

Is it possible to change the header height of an NSTextView?

I am saving a PDF from an NSTextview and putting a logo in the header. I overrode pageHeader and the logo appears but it is clipped.
Is it possible to change the header height of an NSTextView?
Thanks!
Partial code:
-(IBAction)impLaudo:(id)sender
{
NSPrintInfo *printInfo;
NSPrintInfo *sharedInfo;
NSPrintOperation *printOp;
NSMutableDictionary *printInfoDict;
NSMutableDictionary *sharedDict;
sharedInfo = [NSPrintInfo sharedPrintInfo];
sharedDict = [sharedInfo dictionary];
printInfoDict = [NSMutableDictionary dictionaryWithDictionary:sharedDict];
[printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
[printInfoDict setObject:[[dirLaudos stringByAppendingString:[estudo stringValue]] stringByAppendingString:#".pdf"] forKey:NSPrintSavePath];
printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
[printInfo setHorizontalPagination: NSClipPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];
[[printInfo dictionary] setValue:[NSNumber numberWithBool:YES] forKey:NSPrintHeaderAndFooter];
printOp = [NSPrintOperation printOperationWithView:textView printInfo:printInfo];
[printOp setShowsPrintPanel:NO];
[printOp runOperation];
}
#implementation MyTextView
- (NSAttributedString *)pageHeader
{
// Adicionando cabeçalho
NSAttributedString *theHeader = nil;
NSImage * pic = [[NSImage alloc] initWithContentsOfFile:[dirLayout stringByAppendingString:#"cabecalho.jpg"]];
NSTextAttachmentCell *attachmentCell = [[NSTextAttachmentCell alloc] initImageCell:pic];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setAttachmentCell: attachmentCell ];
theHeader = [NSAttributedString attributedStringWithAttachment: attachment];
return theHeader;
}
#end
Instead of overriding -pageHeader, you should override -drawPageBorderWithSize:, which allows you to draw additional marks on the page at print time.
The Size parameter is an NSSize struct containing the size of the current logical page. All you need to do is draw your logo in the correct location:
- (void)drawPageBorderWithSize:(NSSize)pageSize
{
[super drawPageBorderWithSize:pageSize];
//draw your logo
NSPoint offset = NSMakePoint(100.0, 100.0);
NSImage* logo = [NSImage imageNamed:#"logo"];
NSSize logoSize = [logo size];
NSPoint imageOrigin = NSMakePoint(offset.x, pageSize.height - (offset.y + logoSize.height));
[self lockFocus];
[logo drawInRect:NSMakeRect(imageOrigin.x, imageOrigin.y, logoSize.width, logoSize.height)
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0
respectFlipped:YES
hints:nil];
[self unlockFocus];
}

Load an image to UIImage from a file path to the asset library

Related to my previous question here.
I have a path to an image in the asset library , for example:
assets-library://asset/asset.JPG?id=1000000001&ext=JPG
Now, how could I load the image from this path to a UIImage object?
Here is the code:
NSString *path = [occasion imagePath];
//temp
NSLog(#"the 2nd occasion imagePath is: %#", path);
//end
if (path != nil)
{
//UIImage *image = [UIImage imageNamed:path];
UIImage *image = [UIImage imageWithContentsOfFile:path];
image = [image imageByScalingAndCroppingForSize:CGSizeMake(36.0, 42.0)];
[[cell imageView] setImage:image];
}
from the answer linked in my comment above:
-(void)findLargeImage
{
NSString *mediaurl = [self.node valueForKey:kVMMediaURL];
//
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
largeimage = [UIImage imageWithCGImage:iref];
[largeimage retain];
}
};
//
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(#"booya, cant get image - %#",[myerror localizedDescription]);
};
if(mediaurl && [mediaurl length] && ![[mediaurl pathExtension] isEqualToString:AUDIO_EXTENSION])
{
[largeimage release];
NSURL *asseturl = [NSURL URLWithString:mediaurl];
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:asseturl
resultBlock:resultblock
failureBlock:failureblock];
}
}
Try the following:
(UIImage *)imageWithContentsOfFile:(NSString *)path
You also can use [myasset aspectRatioThumbnail] to get your image.

Lazy loading of PhotoLibrary Images

i found an issue with Photo Library Images. It not displaying first time in my View,Image View is blank while loading first time.
Because i found Asset Library block working on another thread.After reloading my View ,I can see all the Images. However first time the Image Views are Blank.
can any one tell me a good way to deal with the problem
It working with Bundle Images.
also some times console shows that
app is crashing due to Program received signal: “0”. Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")
My Code:
for (int j = 0; j<9; j++)
{
//allocating View
UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(xCordImage, yCordImage, 200, 190)];
// allocating ImageView
imageViewTopic = [[[UIImageView alloc] init] autorelease];
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *images;
if (iref) {
images = [UIImage imageWithCGImage:iref];
}
else {
images = [UIImage imageNamed:#"Nofile.png"];
}
imageViewTopic .image = images ;
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
imageViewTopic .image = [UIImage imageNamed:#"Nofile.png"];
NSLog(#"booya, cant get image - %#",[myerror localizedDescription]);
};
NSString *string ;
MyClass *obj = [imageFileNameArray objectAtIndex:j];
**//obj.fileName contains ALAsset URL of a Image**
string = obj.fileName;
NSURL *asseturl = [NSURL URLWithString:string];
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:asseturl resultBlock:resultblock
failureBlock:failureblock];
imageViewTopic.userInteractionEnabled = YES;
imageViewTopic.frame = CGRectMake(0,0, 200, 150);
[currentView addSubview:scroller];
**// adding the imageView to View**
[smallView addSubview:imageViewTopic];
[myView addSubview:smallView];
[scroller addSubview:myView];
}
I am using this method to show images in scroll view with lazy loading. It works well.
First initialize the value of j1. And data is the image data coming from loop from an array.
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL:url];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
__block int j1=_j;
// WARNING: is the cell still using the same data by this point??
// NSURL *url = [NSURL URLWithString: imageName];
UIImage *image = [UIImage imageWithData: data]; //image.size.height
image1=[[UIImageView alloc] initWithFrame:CGRectMake(j1,10,image.size.width,image.size.height)];
image1.image=image;
CALayer *layer = [image1 layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:0.0]; //note that when radius is 0, the border is a rectangle
[layer setBorderWidth:3.0];
[layer setBorderColor:[[UIColor whiteColor] CGColor]];
[portfolio_scroll addSubview:image1];
});
});
_j = _j+ 320;