array of images in document directory - ios

I have select multiple images from iPhone through ELC controller. The images are stored in array now I want to store this array of images in document directory so please help me someone..
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
if ([self respondsToSelector:#selector(dismissViewControllerAnimated:completion:)]){
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self dismissModalViewControllerAnimated:YES];
}
for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for(NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
self.chosenImages = images;
NSLog(#"values=%#",_chosenImages);
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}

For each of the images in the images array, write to the file file one by one.
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSInteger anIndex = 0;
for (UIImage *anImage in images) {
NSString *anImageName = [NSString stringWithFormat:#"%d.png", anIndex++];
NSString *anImagePath = [NSString stringWithFormat:#"%#/%#", aDocumentsDirectory, anImageName];
NSData *anImageData = UIImagePNGRepresentation(anImage);
[anImageData writeToFile:anImagePath atomically:YES];
}
2.. When you extract the Original Image, save image to file right there.
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSInteger anIndex = 0;
for(NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
// Save Image
NSString *anImageName = [NSString stringWithFormat:#"%d.png", anIndex++];
NSString *anImagePath = [NSString stringWithFormat:#"%#/%#", aDocumentsDirectory, anImageName];
NSData *anImageData = UIImagePNGRepresentation(image);
[anImageData writeToFile:anImagePath atomically:YES];
}

Related

Convert NSData To UIImage

First of all I am selecting some images in one listingstep1view and passing those UIImages other View Controller by using sharedInstance.
ListingStep1Viewcontroller:
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
NSLog(#"info :%#", info);
CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;
NSMutableArray *tempArray =[[NSMutableArray alloc] init];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for (int i =0; i<info.count; i++) {
NSDictionary *dict = [info objectAtIndex:i];
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage])
{
UIImage* image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[iPhoneXMPPAppDelegate load_uploadingImage:[image fixOrientation]];
NSData* data = UIImageJPEGRepresentation([iPhoneXMPPAppDelegate get_upload_image], 1.0f);
//NSString *encodedImage = [data base64Encoding];
[Base64 initialize];
self.convertedImage=[[UIImage alloc]initWithData:data];
//self.convertedImage=[UIImage imageWithData:data];
NSLog(#"converted Data To Image= %#",self.convertedImage);
NSString *strEncoded = [Base64 encode:data];
strEncoded = [strEncoded stringByReplacingOccurrencesOfString:#"+" withString:#"%2B"];
[tempArray addObject:strEncoded];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
} else {
NSLog(#"UIImagePickerControllerReferenceURL = %#", dict);
}
}
else if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypeVideo){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
} else {
NSLog(#"UIImagePickerControllerReferenceURL = %#", dict);
}
} else {
NSLog(#"Uknown asset type");
}
}
self.chosenImages = images;
[[ListingStep2Model sharedInstance]setImagesAssetArray:tempArray];
//[[ListingStep2Model sharedInstance]setConvertedImage:self.convertedImage.image];
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}
ListingStep5viewcontoller:
for (int i = 0; i < [[[ListingStep2Model sharedInstance] imagesAssetArray]count]; i++) {
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[[[ListingStep2Model sharedInstance] imagesAssetArray]count]];
//UIImage*imageTodata=[[ListingStep2Model sharedInstance] convertedImage];
NSLog(#"Data Imges:%#", [[[ListingStep2Model sharedInstance] imagesAssetArray] objectAtIndex:i]);
//NSLog(#"Data Imges:%#", imageTodata);
NSDictionary *dict;
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage])
{
{
UIImage* image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[iPhoneXMPPAppDelegate load_uploadingImage:[image fixOrientation]];
NSData* data = UIImageJPEGRepresentation([iPhoneXMPPAppDelegate get_upload_image], 1.0f);
//UIImageView *imgv = [[UIImageView alloc]init];
[Base64 initialize];
NSString *strEncoded = [Base64 encode:data];
strEncoded = [strEncoded stringByReplacingOccurrencesOfString:#"+" withString:#"%2B"];
NSMutableDictionary *param = [NSMutableDictionary dictionary];
[param setObject:strEncoded forKey:[NSString stringWithFormat:#"image%d",i]];
}
} else {
NSLog(#"UIImagePickerControllerReferenceURL = %#", dict);
}
}
}
[iPhoneXMPPAppDelegate startIndicator];
NSMutableArray *totalrequest = [[NSMutableArray alloc] init];
[totalrequest addObject:param];
[RequestAndResponseHandlers helparucommonRequestAndResponseHandlerWithParam:[NSDictionary dictionaryWithObjectsAndKeys:totalrequest,#"request",nil] methodName:methodName completionHandler:^(NSDictionary *response) {
NSLog(#"Response :%#", response);
if ([[response valueForKeyPath:#"response.httpCode"] integerValue]==200) {
[[iPhoneXMPPAppDelegate appdelegate] navigateTo:[iPhoneXMPPAppDelegate publicView ]];
}
else {
[iPhoneXMPPAppDelegate alertWithTitle:#"Alert" message:#"Please try again!"];
}
[iPhoneXMPPAppDelegate stopIndicator];
[self.containerTbl reloadData];
}];
}
Output is in NSData Format:
4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAEcKADAAQAAAABAAAC9gAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs%2BEJ%2B/8AAEQgC9gRwAwERAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5%2Bv/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZ
First you need to change the Base64string to NSData and then you can easily convert to UIImage. Here are the steps:
(1) Convert Base64String to NSData using the NSData+Base64 files.
NSData *imageData = [NSData dataFromBase64String:originalString];
(2) Now you can convert the NSData to Image as follows:
UIImage *newImage = [UIImage imageWithData:imageData];

How to get playable video url by selecting a video using ELCImagePickerController

Right now, I am selecting multiple video using ELCImagePickerController But after selection it give ALAssetTypeVideo not public.movie" type url , So I am unable to play this with AVPlayer.
Please help me.
I am trying with Following code:
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info) {
NSURL *urlvideo = [dict objectForKey:UIImagePickerControllerReferenceURL];
NSLog(#"Video URl is::::--%#",urlvideo);
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
self.chosenImages = images;
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}
I've solved my problem by saving video firstly in temp file and then use url of video which is stored in temp file,Now Video is playing properly
+(NSString *) videoAssetURLToTempFile:(NSURL*)url
{
NSString * surl = [url absoluteString];
NSString * ext = [surl substringFromIndex:[surl rangeOfString:#"ext="].location + 4];
NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate];
NSString *str = [NSString stringWithFormat:#"%f",ti];
NSArray *Array = [str componentsSeparatedByString:#"."];
NSString *fileString =[[NSString alloc]initWithString:[Array objectAtIndex:0]];
NSLog(#"FileName string is ::::--%#",fileString);
NSString * filename = [NSString stringWithFormat: #"%#.%#",fileString,ext];
NSLog(#"FileName is ::::--%#",filename);
NSString * tmpfile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation * rep = [myasset defaultRepresentation];
NSUInteger size = [rep size];
const int bufferSize = 8192;
//NSLog(#"Writing to %#",tmpfile);
FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+");
if (f == NULL) {
NSLog(#"Can not create tmp file.");
return;
}
Byte * buffer = (Byte*)malloc(bufferSize);
int read = 0, offset = 0, written = 0;
NSError* err;
if (size != 0) {
do {
read = [rep getBytes:buffer
fromOffset:offset
length:bufferSize
error:&err];
written = fwrite(buffer, sizeof(char), read, f);
offset += read;
} while (read != 0);
}
fclose(f);
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(#"Can not get asset - %#",[myerror localizedDescription]);
};
if(url)
{
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:url
resultBlock:resultblock
failureBlock:failureblock];
}
return tmpfile;
}
for more detail refer Getting video from ALAsset

NSOperationQueue Issue

I have a code that i used as a image downloder in ios 6 but when i use same code in different application that used ios 5 it gives me error NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance hear is the code that i am using
[self.imageDownloadingQueue addOperationWithBlock:^{
NSURL *imageUrl = [NSURL URLWithString:ImageURL];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *images = nil;
if (imageData)
images = [UIImage imageWithData:imageData];
if (images){
[self.imageCache setObject:images forKey:ImageURL];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// make sure the cell is still visible
imageV.image = images;
}];
}
}];
Can anybody tell me what i am doing wrong coz this code is help me a lot when downloading image separately. Thank you
PS Whole function that i am using
-(void) setScrollView:(UIScrollView *)scrollview :(NSArray *)contentArray{
int x = 0;
int i = 0;
NSString *ImageURL=#"http://www.online-image-editor.com/styles/2013/images/example_image.png";
for(NSDictionary *str in contentArray){
UIImageView *imageV = [[UIImageView alloc ] initWithFrame:CGRectMake(x, 0, 200,125)];
#try {
ImageURL = [str objectForKey:#"image"];
}
#catch (NSException * e) {
NSLog(#"Exception: %#", [e description]);
}
imageV.userInteractionEnabled = YES;
ImageTapGesture *singleTap = [[ImageTapGesture alloc] initWithTarget:self action:#selector(tapDetectedMain:)];
singleTap.newItem = str;
singleTap.currentItem = i ;
singleTap.numberOfTapsRequired = 1;
[imageV addGestureRecognizer:singleTap];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(x,125,200,20)];
[title setFont:[UIFont systemFontOfSize:11]];
title.numberOfLines = 2;
title.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[title setBackgroundColor:[UIColor blackColor]];
[title setTextColor:[UIColor whiteColor]];
title.text = [NSString stringWithFormat:#" %#",[str objectForKey:#"title"]];
x += title.frame.size.width+2;
UIImage *cachedImage = [self.imageCache objectForKey:ImageURL];
if (cachedImage){
imageV.image = cachedImage;
}
else{
imageV.image = [UIImage imageNamed:#"vdo.png"];
[self.imageDownloadingQueue addOperationWithBlock:^{
NSURL *imageUrl = [NSURL URLWithString:ImageURL];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *images = nil;
if (imageData)
images = [UIImage imageWithData:imageData];
if (images){
[self.imageCache setObject:images forKey:ImageURL];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// make sure the cell is still visible
imageV.image = images;
}];
}
}];
}
[scrollview addSubview:imageV];
[scrollview addSubview:title];
}
scrollview.contentSize = CGSizeMake(x, scrollview.frame.size.height);
i++;
}
You should definitely check the ImageURL you are sending into the operation. It seems, this ImageURL is [NSNull null].
If it is a local variable, is it declared as __block NSString *ImageUrl?
pls try this code:
[self.imageDownloadingQueue addOperationWithBlock:^{
NSURL *imageUrl ;
if((ImageURL!=nil)&&(![ImageURL isKindOfClass:[NSNull class]]))
{
imageUrl= [NSURL URLWithString:ImageURL];
}
NSData *imageData;
if((imageUrl!=nil)&&(![imageUrl isKindOfClass:[NSNull class]]))
{
imageData = [NSData dataWithContentsOfURL:imageUrl];
}
UIImage *images = nil;
if((imageData!=nil)&&(![imageData isKindOfClass:[NSNull class]]))
images = [UIImage imageWithData:imageData];
if((images!=nil)&&(![images isKindOfClass:[NSNull class]]))
{
[self.imageCache setObject:images forKey:ImageURL];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// make sure the cell is still visible
imageV.image = images;
}];
}
}];
Hope this will help you.

ViewDidLoad to load 4 images

I have tried this in my ViewDidLoad:
NSString *myGrabbedImage = #"myGrabbedImage.png";
NSString *myGrabbedImage2 = #"myGrabbedImage2.png";
NSString *myGrabbedImage3 = #"myGrabbedImage3.png";
NSString *myGrabbedImage4 = #"myGrabbedImage4.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *path3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *path4 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *documentDirectory2 = [path2 objectAtIndex:0];
NSString *documentDirectory3 = [path3 objectAtIndex:0];
NSString *documentDirectory4 = [path4 objectAtIndex:0];
NSString *fullPath = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
NSString *fullPath2 = [documentDirectory2 stringByAppendingPathComponent:myGrabbedImage2];
NSString *fullPath3 = [documentDirectory3 stringByAppendingPathComponent:myGrabbedImage3];
NSString *fullPath4 = [documentDirectory4 stringByAppendingPathComponent:myGrabbedImage3];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
NSData *data2 = [NSData dataWithContentsOfFile:fullPath2];
NSData *data3 = [NSData dataWithContentsOfFile:fullPath3];
NSData *data4 = [NSData dataWithContentsOfFile:fullPath4];
if ([myGrabbedImage isEqualToString:#"myGrabbedImage.png"])
{
[[self firstImageView]setImage:[UIImage imageWithData:data]];
}
else if ([myGrabbedImage2 isEqualToString:#"myGrabbedImage2.png"])
{
[[self secondImageView]setImage:[UIImage imageWithData:data2]];
}
else if ([myGrabbedImage3 isEqualToString:#"myGrabbedImage3.png"])
{
[[self thirdImageView]setImage:[UIImage imageWithData:data3]];
}
else
{
[[self fourthImageView]setImage:[UIImage imageWithData:data4]];
}
Then I have this in my UIImagePickerController method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data = UIImagePNGRepresentation(image1);
NSString *myGrabbedImage = #"myGrabbedImage.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
[data writeToFile:fullPathToFile atomically:YES];
if (picker == imagePickerController)
{
[[self firstImageView]setImage:image1];
}
else if (picker == imagePickerController2)
{
[[self secondImageView]setImage:image1];
}
else if (picker == imagePickerController3)
{
[[self thirdImageView]setImage:image1];
}
else if (picker == imagePickerController4)
{
[[self fourthImageView]setImage:image1];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
of coarse, the ViewDidLoad just shows one image. But how can I make it load all the 4 UIImageView´s.
Thanks in advance
#import <UIKit/UIKit.h>
#interface PickerController: UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
NSArray *titleStrings;
UIPickerView *picker;
UIImage *imageView;
}
#end
#implementation PickerController
- (id) init {
if (self = [super init]) {
titleStrings = [[NSArray alloc] initWithObjects:#"A", #"B", NULL];
picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
picker.showsSelectionIndicator = YES;
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"empty.png"]];
imageView.frame = CGRectMake(0, 200, imageView.frame.size.width, imageView.frame.size.height);
[self.view addSubview:imageView];
[imageView release];
[picker release];
}
return self;
}
- (void) dealloc {
[titleStrings release];
[super dealloc];
}
/* UIPickerViewDataSource */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
return 2;
}
/* UIPickerViewDelegate */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [titleStrings objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:( NSInteger)component {
NSString *first = [titleStrings objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *second = [titleStrings objectAtIndex:[pickerView selectedRowInComponent:1]];
NSString *imageName = [[NSString alloc] initWithFormat:#"%#%#", first, second];
imageView.image = [UIImage imageNamed:imageName];
[imageName release];
[imageView sizeToFit];
}
#end

NSArray Image doesn't show in ScrollView

NSArray image have:
2012-08-15 10:12:39.687 test[4200:11103] (
"http://192.168.1.165/Images/Demo/1.jpg",
"http://192.168.1.165/Images/Demo/0.jpg",
"http://192.168.1.165/Images/Demo/0.jpg",
"http://192.168.1.165/Images/Demo/2.jpg",
"http://192.168.1.165/Images/Demo/2.jpg",
"http://192.168.1.165/Images/Demo/2.jpg",
"http://192.168.1.165/Images/Demo/2.jpg",
"http://192.168.1.165/Images/Demo/1.jpg",
"http://192.168.1.165/Images/Demo/1.jpg",
"http://192.168.1.165/Images/Demo/1.jpg" )
And code .m:
dispatch_async(htvque, ^{
NSData* data = [NSData dataWithContentsOfURL: tophotfilm];
NSError* error;
json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
NSDictionary *list = [json objectForKey:#"List"];
NSMutableArray *arrPoster =[[NSMutableArray alloc]init];
for(NSString *po in list)
{
NSString *poster = [po valueForKey:#"Poster"];
[arrPoster addObject:poster];
}
myArray = [NSArray arrayWithArray:arrPoster];
NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor redColor],
[UIColor greenColor], [UIColor magentaColor],
[UIColor blueColor], [UIColor orangeColor],
[UIColor brownColor], [UIColor grayColor], nil];
//NSLog(#"%#",colors);
NSLog(#"%#",myArray);
for (int i = 0; i < myArray.count; i++) {
//NSLog(#"%#",myArray.count);
NSString *imageName = [NSString stringWithFormat:#"%#", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.origin.x = self.scrollView.frame.size.width * i;
rect.origin.y = 0;
rect.size = scrollView.frame.size;
[self.scrollView addSubview:imageView];
}
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *
myArray.count,scrollView.frame.size.height);
pageControl.currentPage = 0;
pageControl.numberOfPages = myArray.count;
[super viewDidLoad];
});
With NSArray colors, it's working ok, but NSArray image has some error.
scrollview don't display listimage, it show first image, anybody know why...
dispatch_async(dispatch_get_main_queue(), ^{
for (int i = 0; i < myArray.count; i++)
{
//NSString *imageName = [NSString stringWithFormat:#"%#",[myArray objectAtIndex:i]];
NSURL *urlImage = [NSURL URLWithString:[NSString stringWithFormat:#"%#",[myArray objectAtIndex:i]]];
//NSLog(#"%#",urlImage);
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:urlImage]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = imgHeight;
rect.size.width = imgWidth;
imageView.frame = rect;
imageView.tag = i;
//rect.size = scrollView.frame.size;
[scrollView addSubview:imageView];
}
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *
myArray.count,scrollView.frame.size.height);
pageControl.currentPage = 0;
pageControl.numberOfPages = myArray.count;
});
Check this line of code
NSString *imageName = [NSString stringWithFormat:#"%#", i];
You image name would be just 1, 2 etc., not the image names printed in the log,
It should be something like
[myArray objectATIndex:i];
You are using imageNamed: which can only be used for images inside your project, and if I understood your question you are trying to access them from a server. Try this UIImage method: imageWithData:
NSURL *url = [NSURL URLWithString:[myArray objectAtIndex:i]];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

Resources