iCarousel + Parse: Loading Async Images from PFObject - ios7

I am trying to populate an iCarousel with a parse (parse.com, pfoject) object without success.
I can't find any example of how to do it, so it why i am asking here...
- (void)viewDidLoad
{
[super viewDidLoad];
_items = [NSMutableArray array];
PFQuery *query = [PFQuery queryWithClassName:#"testUser"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded. The first 100 objects are available in objects
[_items addObjectsFromArray:objects];
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
//configure carousel
_carousel.type = iCarouselTypeRotary;
//_carousel.viewpointOffset = CGSizeMake(0.0f, 100.0f);
[_carousel setContentOffset:CGSizeMake(0.0f, -60.0f)];
}
And at viewForItemAtIndex:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300.0f, 300.0f)];
}
PFObject *eachObject = [_items objectAtIndex:index];
PFFile *theImage = [eachObject objectForKey:#"image"];
NSData *imageData = [theImage getData];
UIImage *image = [UIImage imageWithData:imageData];
((UIImageView *)view).image = image;
view.contentMode = UIViewContentModeCenter;
return view;
}
Please, any help? :-(

In viewDidLoad, After the
[_items addObjectsFromArray:objects];
Put
[_carousel reloadData];
Otherwise the carousel won't be updated after the items have been added to the array, so you'll still be looking at an empty carousel.

Related

This item cannot be shared. Please select a different item when sharing on whatsApp

first time app install and share image is not working, it show alert:
This item cannot be shared. Please select a different item.
and I will try second time image share successfully. What is the issue?
-(void)TwitterAndmanyMore
{
UIImage * image = _imageView.image;
NSArray * items = #[image];
UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
// and present it
[self presentActivityController:controller];
}
- (void)presentActivityController:(UIActivityViewController *)controller {
// for iPad: make the presentation a Popover
controller.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:controller animated:YES completion:nil];
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionAny;
popController.barButtonItem = self.navigationItem.leftBarButtonItem;
// access the completion handler
controller.completionWithItemsHandler = ^(NSString *activityType,
BOOL completed,
NSArray *returnedItems,
NSError *error)
{
// react to the completion
NSLog(#"----retu------%#",returnedItems);
if (completed)
{
[self showContine];
//[self viewWillAppear:YES];
// user shared an item
NSLog(#"We used activity type%#", activityType);
} else {
// user cancelled
NSLog(#"We didn't want to share anything after all.");
}
if (error) {
NSLog(#"An Error occured: %#, %#", error.localizedDescription, error.localizedFailureReason);
}
};
}
use below code for this issue it work
-(void)TwitterAndmanyMore
{
// UIImage * image = _imageView.image;
// NSLog(#"Image Data %#",image);
// NSArray * items = #[#"", image];
tempImage = [UIImage imageNamed:#"Sample.jpg"];
tempImage = _imageView.image;
NSString *theMessage = #"";
NSArray *items;
CGImageRef cgref = [tempImage CGImage];
CIImage *cim = [tempImage CIImage];
if (cim != nil || cgref != NULL)
{
items = #[theMessage,tempImage];
}
else
{
items = #[theMessage];
}
NSLog(#"Image Data %#",items);
// build an activity view controller
UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
// and present it
[self presentActivityController:controller];
}
- (void)presentActivityController:(UIActivityViewController *)controller {
// for iPad: make the presentation a Popover
controller.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:controller animated:YES completion:nil];
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionAny;
popController.barButtonItem = self.navigationItem.leftBarButtonItem;
// access the completion handler
controller.completionWithItemsHandler = ^(NSString *activityType,
BOOL completed,
NSArray *returnedItems,
NSError *error)
{
// react to the completion
NSLog(#"----retu------%#",returnedItems);
if (completed)
{
[self showContine];
//[self viewWillAppear:YES];
// user shared an item
NSLog(#"We used activity type%#", activityType);
} else {
// user cancelled
NSLog(#"We didn't want to share anything after all.");
}
if (error) {
NSLog(#"An Error occured: %#, %#", error.localizedDescription, error.localizedFailureReason);
}
};
}

IOS - How to retrieve images and strings from parse and set UIImageview and UIlabel in UICollectionView

Ok so Im using storyboards and Trying to add String data that goes with the images
the images are in the same row with the name of the picture in parse
I got the image and was able to put it on a UICollectionView im just having trouble connecting the name that goes with it in the same cell
.
how do i get a different name that goes with the image for each cell
- (void)viewDidLoad {
[super viewDidLoad];
[self queryParseMethod];
self.userCollection.alwaysBounceVertical = YES;
}
- (void)queryParseMethod {
PFQuery *query = [PFQuery queryWithClassName:#"imageandtitle"];
[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
if (!error) {
imagefilesArray = [[NSArray alloc] initWithArray:objects];
[userCollection reloadData];
}
}];
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [imagefilesArray count];
}
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
userCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"userCard" forIndexPath:indexPath];
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
self.affiliates = objects;
for(NSDictionary *affiliate in self.affiliates) {
//cell.Name.text = affiliate[#"Name"];
NSArray* array = [[NSArray alloc] initWithObjects:affiliate[#"Name"], nil];
for (NSString *name in array) {
PFObject *imageObject = [array objectAtIndex:indexPath.row];
cell.Name.text = name;
}
}
}
}];
PFObject *imageObject = [imagefilesArray objectAtIndex:indexPath.row];
PFFile *imageFile = [imageObject objectForKey:#"imageFile"];
PFObject *nameofUser = [imageObject objectForKey:#"Name"];
[imageFile getDataInBackgroundWithBlock:^(NSData * _Nullable data, NSError * _Nullable error) {
if (!error) {
cell.userPI.image = [UIImage imageWithData:data];
}
}];
return cell;
}
I Am currently only getting one name and if i try to uncomment the other cell.text i still get one name but in all cells.
Thank you in advanced.
sorry guys the answer was really simple i just had to remove the for loop and query since i already queried as a method then type
cell.Name.text = [imageObject objectForKey:#"Name"];
thank you for your time.

UICollectionView Memory Leak Crash

My CollectionView is retrieving images from the Parse network, and keeps crashing. I keep getting memory warnings right before the crash. I'm guessing I need to free up space. But the problem is i'm relatively new to coding so I don't know how to do this. I don't even know where the memory needs to be freed from. Could someone please help me understand what I am doing wrong in my code?
- (void)viewDidLoad {
[super viewDidLoad];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavBar1.png"] forBarMetrics:UIBarMetricsDefault];
// self.navigationController.hidesBarsOnSwipe = true;
[self queryParseMethodCell];
// Do any additional setup after loading the view.
}
- (void)queryParseMethodCell {
NSLog(#"start Money");
PFQuery *query3 = [PFQuery queryWithClassName:#"MainWall"];
PFQuery *query4 = [PFQuery queryWithClassName:#"MainWall"];
[query3 orderByDescending:#"createdAt"];
[query4 orderByAscending:#"createdAt"];
[query3 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"check bobby");
imageFilesArray2 = [[NSArray alloc] initWithArray:objects];
[self.HomePost reloadData];
}
else{
NSLog(#"NO Good33");
}
}];
[query4 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"check bobby");
imageFilesArray3 = [[NSArray alloc] initWithArray:objects];
[self.HomePost reloadData];
}
else{
NSLog(#"NO Good44");
}
}];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.lastContentOffset > scrollView.contentOffset.y) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
else if (self.lastContentOffset < scrollView.contentOffset.y){
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
else {
NSLog(#"Andy Error1");
}
}
- (IBAction)SwipeLeft:(UISwipeGestureRecognizer *)sender {
[self performSegueWithIdentifier:#"Notifications" sender:self];
}
- (IBAction)SwipeRight:(UISwipeGestureRecognizer *)sender{
[self performSegueWithIdentifier:#"FavWall" sender:self];
}
- (IBAction)MainWall:(id)sender{
[self performSegueWithIdentifier:#"MainWall" sender:self];
}
- (IBAction)FavWall:(id)sender{
[self performSegueWithIdentifier:#"FavWall" sender:self];
}
- (IBAction)Notifications:(id)sender{
[self performSegueWithIdentifier:#"Notifications" sender:self];
}
- (IBAction)Home:(id)sender{
[self performSegueWithIdentifier:#"Home" sender:self];
}
- (IBAction)YourAccount:(id)sender{
[self performSegueWithIdentifier:#"YourAccount" sender:self];
}
- (IBAction)UsersAccount:(id)sender{
[self performSegueWithIdentifier:#"UsersAccount" sender:self];
}
-(IBAction)PicTake:(id)sender{
[self performSegueWithIdentifier:#"Camera" sender:self];
}
#pragma mark Collection View
-(NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [imageFilesArray2 count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"TopCell";
DiscoverCell *cell = nil;
if (cell == nil) {
cell = (DiscoverCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
}
else {
NSLog(#"2Error");
}
PFObject *imageObject2 = [imageFilesArray2 objectAtIndex:indexPath.row];
cell.Username.text = [imageObject2 objectForKey:#"Name"];
PFFile *imageFile3 = [imageObject2 objectForKey:#"ProfilePicture"];
[imageFile3 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSLog(#"CheckCash");
// cell.TopStory.image = [UIImage imageWithData:data];
cell.HomeImg.image = [UIImage imageWithData:data];
cell.Back.image = [UIImage imageWithData:data];
//cell.HomeImg2.image = [UIImage imageWithData:data];
}
else{
NSLog(#"NO Good");
}
cell.HomeImg.layer.cornerRadius = cell.HomeImg.frame.size.width /2;
cell.HomeImg.clipsToBounds = YES;
cell.Back.image = cell.HomeImg.image;
}];
PFObject *imageObject5 = [imageFilesArray3 objectAtIndex:indexPath.row];
cell.Username.text = [imageObject5 objectForKey:#"Name"];
PFFile *imageFile4 = [imageObject5 objectForKey:#"ProfilePicture"];
[imageFile4 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSLog(#"CheckCash");
cell.TopStory.image = [UIImage imageWithData:data];
}
else{
NSLog(#"NO Good22");
}
}];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You have a memory leak. Everytime you scroll all images all reloaded. Something like this should solve the problem
//at start For every data DataisNotLoaded should be true
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if dataisNotLoaded[index] {
DataisNotLoaded[index] = false
[imageFile3 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSLog(#"CheckCash");
// cell.TopStory.image = [UIImage imageWithData:data];
cell.HomeImg.image = [UIImage imageWithData:data];
cell.Back.image = [UIImage imageWithData:data];
//cell.HomeImg2.image = [UIImage imageWithData:data];
}
else{
NSLog(#"NO Good");
}
}

How to display video from ALAsset to UICollectionview Cell

I tried this code to add all ALAsset created video into my app and then play it. But the video doesn't show in UICollectionView. How is it possible?
I write this code in View Did Load.
_collectionView.dataSource=self;
_collectionView.delegate=self;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
_allVideos = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
_dic = [[NSMutableDictionary alloc] init];
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
NSURL *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
NSString *title = [NSString stringWithFormat:#"video %d", arc4random()%100];
UIImage *image = [self imageFromVideoURL:videoURL];
[_dic setValue:image forKey:#"image"];
[_dic setValue:title forKey:#"name"];
[_dic setValue:videoURL forKey:#"url"];
//[_allVideos addObject:_dic];
[_allVideos addObject:asset];
[_collectionView reloadData];
}
}];
}
}
failureBlock:^(NSError *error)
{
NSLog(#"error enumerating AssetLibrary groups %#\n", error);
}];
}
And One method is
- (UIImage *)imageFromVideoURL:(NSURL*)videoURL
{
// result
UIImage *image = nil;
// AVAssetImageGenerator
AVAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];;
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
// calc midpoint time of video
Float64 durationSeconds = CMTimeGetSeconds([asset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
// get the image from
NSError *error = nil;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
if (halfWayImage != NULL)
{
// CGImage to UIImage
image = [[UIImage alloc] initWithCGImage:halfWayImage];
[_dic setValue:image forKey:#"name"];
NSLog(#"Values of dictionary==>%#", _dic);
NSLog(#"Videos Are:%#",videoURL);
CGImageRelease(halfWayImage);
}
return image;
}
And I wrote this in UICollectionView:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _allVideos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
NSLog(#"allvideo %#", _allVideos);
ALAsset *alasset = [_allVideos objectAtIndex:indexPath.row];
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
imageview.image = [UIImage imageWithCGImage:alasset.thumbnail];
[cell.contentView addSubview:imageview];
return cell;
}
Write the code in ViewDidLoad
assets = [[NSMutableArray alloc] init];
_library = [[ALAssetsLibrary alloc] init];
UIImage *viewImage;
[_library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(#"error");
} else {
NSLog(#"url %#", assetURL);
}
}];
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){
if (group != NULL) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
if ([[result valueForProperty:ALAssetsGroupPropertyName] isEqual:#"VideoMaker"]) {
NSLog(#"asset: %#", result);
[assets addObject:result];
}
}];
}
[self.collectionView reloadData];
//[self.activity stopAnimating];
//[self.activity setHidden:YES];
}
failureBlock:^(NSError *error){
NSLog(#"failure"); }];`} `
In this assets is NSMutableArray and library is the ALAssetLibrary.
In UIcollectionviewcell cell for rowAtIndexpath method
-(VideoCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
VideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CELL" forIndexPath:indexPath];
ALAsset *asset = [assets objectAtIndex:indexPath.row];
[cell.videoImageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
return cell;
}

I have some remote images I'm loading into an NSMutableArray but the array isn't populated by the time I need it

Well after troubles with swipe gesture and my own image gallery slider I decided to go with a premade one.
I'm using iCarousel and it works fine. I've tested using an array that is populated with non remote images.
Inside init with coder I done this:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
_additionalGarmentImagesArray = [[NSMutableArray alloc] init];
UIImage *image1 = [UIImage imageNamed:#"asos.png"];
UIImage *image2 = [UIImage imageNamed:#"neck.png"];
UIImage *image3 = [UIImage imageNamed:#"diamind.png"];
[_additionalGarmentImagesArray addObject:image1];
[_additionalGarmentImagesArray addObject:image2];
[_additionalGarmentImagesArray addObject:image3];
}
return self;
}
This works fine. However it's not useful to me as my images are remote images. So I've taken the code I used with my own image gallery but the images don't get downloaded on time.
In my viewDidLoad:
PFQuery *query = [PFQuery queryWithClassName:#"Garments"];
[query whereKey:#"title" equalTo:[self garmentTitle]];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
PFFile *additionalImage1 = [object objectForKey:#"image2"];
PFFile *additionalImage2 = [object objectForKey:#"image3"];
PFFile *additionalImage3 = [object objectForKey:#"image4"];
PFFile *additionalImage4 = [object objectForKey:#"image5"];
PFFile *additionalImage5 = [object objectForKey:#"image6"];
// Add main image to array first, the one passed over from previous controller
[_additionalGarmentImagesArray addObject:[self garmentImage]];
if (additionalImage1) {
PFImageView *pfImageView1 = [[PFImageView alloc] init];
[pfImageView1 setFile:additionalImage1];
[pfImageView1 loadInBackground:^(UIImage *image, NSError *error) {
if (!error) {
[_additionalGarmentImagesArray addObject:image];
}
}];
}
if (additionalImage2) {
PFImageView *pfImageView2 = [[PFImageView alloc] init];
[pfImageView2 setFile:additionalImage2];
[pfImageView2 loadInBackground:^(UIImage *image, NSError *error) {
if (!error) {
[_additionalGarmentImagesArray addObject:image];
}
}];
}
if (additionalImage3) {
PFImageView *pfImageView3 = [[PFImageView alloc] init];
[pfImageView3 setFile:additionalImage3];
[pfImageView3 loadInBackground:^(UIImage *image, NSError *error) {
if (!error) {
[_additionalGarmentImagesArray addObject:image];
}
}];
}
} else {
NSLog(#"empty array");
}
}];
This is where the array of images is needed:
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [_additionalGarmentImagesArray count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
PFImageView *imageView = [[PFImageView alloc] initWithFrame:CGRectMake(0, 0, 300.0f, 380)];
view = imageView;
//set image
((PFImageView *)view).image = _additionalGarmentImagesArray[index];
return view;
}
The viewDidAppear method shows array is populated:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(#"Images Count: %i", [_additionalGarmentImagesArray count]);
}
Please help me solve this issue with solutions. My images are stored remotely and I need a way to have them ready by the time the controller is loaded so they can be used by iCarousel or maybe have iCarousel wait then refresh itself when the array is populated.
Maybe even some how do something in the previous controller which is a UICollectionView. I could tap the cell then get the images I need from the cells object and populate an array then. The only allow the push/segue to happen once the images are in the array. However I don't like that. It means a user would have to wait after they tap to go to the detail view controller where the iCarousel is.
Would appreciate some solutions if possible. It's been a few hours and I've made little progress.
Thanks for your time
Solution was to reloadData at a time where I did have the array. So the best place to do this was inside the viewDidAppear method.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
PFImageView *imageView = [[PFImageView alloc] initWithFrame:CGRectMake(0, 0, 300.0f, 400)];
view = imageView;
//create new view if no view is available for recycling
if (view == nil)
{
//set image
((PFImageView *)view).image = _additionalGarmentImagesArray[index];
} else {
//set image
((PFImageView *)view).image = _additionalGarmentImagesArray[index];
}
return view;
}
Reload Data:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(#"Images Count: %i", [_additionalGarmentImagesArray count]);
[[self carousel] reloadData];
}