GPUImage GPUImageTwoInputFilter two videos - objective-c

I have a problem with GPUImage using GPUImageTwoInputFilter (any filter like GPUImageChromaKeyBlendFilter) for two videos. I have no videos in this case.
#interface ViewController ()
#property (nonatomic) GPUImageMovie *video;
#property (nonatomic) GPUImageMovie *chromaVideo;
#property (nonatomic) GPUImageMovieWriter *movieWriter;
#property (nonatomic) GPUImageColorDodgeBlendFilter *filter;
#end
#implementation ViewController
- (void)viewDidLoad
{
NSURL *videoUrl = [[NSBundle mainBundle] URLForResource:#"video"
withExtension:#"MOV"];
_video = [[GPUImageMovie alloc] initWithURL:videoUrl];
_video.playAtActualSpeed = YES;
NSURL *chromaVideoUrl = [[NSBundle mainBundle] URLForResource:#"chroma"
withExtension:#"mov"];
_chromaVideo = [[GPUImageMovie alloc] initWithURL:chromaVideoUrl];
_chromaVideo.playAtActualSpeed = YES;
_filter = [[GPUImageColorDodgeBlendFilter alloc] init];
//[_filter setThresholdSensitivity:0.45];
[_video addTarget:_filter];
[_chromaVideo addTarget:_filter];
NSURL *outURL = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/MovieMix.mov"]];
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/MovieMix.mov"]
error:nil];
_movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:outURL size:CGSizeMake(1280, 720)];
__weak ViewController *weakSelf = self;
[_filter addTarget:_movieWriter];
[_movieWriter startRecording];
[_video startProcessing];
[_chromaVideo startProcessing];
[_movieWriter setCompletionBlock:^
{
[weakSelf.filter removeTarget:weakSelf.movieWriter];
[weakSelf.movieWriter finishRecording];
UISaveVideoAtPathToSavedPhotosAlbum([NSHomeDirectory() stringByAppendingPathComponent:#"Documents/MovieMix.mov"],nil,nil,nil);
NSLog(#"Done");
}];
}
But when I do operations in completion block with dispatch_after 5 sec, I have a video in my gallery with duration == 0 (I think it has 1 frame).

Related

How to pass NSArray from an NSObject class to a UIViewController class?

I am new to Objective-C. I am trying to create a weather app where I parsed data from open weather map. I have stored the parsed data to an array. Now want to access the array value from other class but getting null value.
Can anyone help me?
What I have tried:
Here is my NSObject class where I am storing data and trying to send that to view controller:
- (void)getCurrentWeather:(NSString *)query
{
NSString *const BASE_URL_STRING = #"http://api.openweathermap.org/data/2.5/weather?q=";
NSString *const API_KEY = #"&APPID=APIKEYSTRING";
NSString *weatherURLText = [NSString stringWithFormat:#"%#%#%#",
BASE_URL_STRING, query,API_KEY];
NSURL *weatherURL = [NSURL URLWithString:weatherURLText];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:weatherURL];
[self performSelectorOnMainThread:#selector(fetchedDataSmile | :) withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSString* cityName = [json objectForKey:#"name"];
int currentTempCelsius = (int)[[[json objectForKey:#"main"] objectForKey:#"temp"] intValue] - ZERO_CELSIUS_IN_KELVIN;
int maxTemp = (int)[[[json objectForKey:#"main"] objectForKey:#"temp_max"] intValue] - ZERO_CELSIUS_IN_KELVIN;
int minTemp = (int)[[[json objectForKey:#"main"] objectForKey:#"temp_min"] intValue] - ZERO_CELSIUS_IN_KELVIN;
NSString *weatherDescription = [[[json objectForKey:#"weather"] objectAtIndexBlush | :O ] objectForKey:#"description"];
weatherArray = [[NSMutableArray alloc] initWithObjects:cityName, weatherDescription,
[NSString stringWithFormat:#"%d", currentTempCelsius],
[NSString stringWithFormat:#"%d", maxTemp],
[NSString stringWithFormat:#"%d", minTemp],nil];
I have NSObject.h file as:
#interface WeatherData : NSObject
#property (nonatomic) NSString *weatherDescription;
#property (strong, nonatomic) NSString *currentTemp;
#property (nonatomic) int maxTempCelsius;
#property (nonatomic) int minTempCelsius;
#property (nonatomic, retain) NSMutableArray *weatherArray;
- (void)getCurrentWeather:(NSString *)query;
#end
In my view controller:
.h file:
#property (nonatomic, retain) NSMutableArray *weatherResultArray;
.m file:
-(void)searchButtonClicked:(UIButton*)sender
{
[self.view endEditing:YES];
WeatherData *weather = [[WeatherData alloc] init];
[weather getCurrentWeather:_textField.text];
self.weatherResultArray = weather.weatherArray;
//temperatureLabel.text = [NSString stringWithFormat:#"%d°",weather.currentTempCelsius];
}
I just want to show the results in UILabel.
Have you tried returning NSMutable array in this method
- (NSMutableArray*)getCurrentWeather:(NSString *)query
instead of this,
- (void)getCurrentWeather:(NSString *)query
This would be the easiest way to verify and also value can be retrieved in single statement as:
self.weatherResultArray = [weather getCurrentWeather:_textField.text];
One more thing, Don't forget to allocate and initialise your weatherResultArray as:
self.weatherResultArray = [[NSMutableArray alloc]init];
In NSObject class, define a weather protocol.
//NSObject.h file
#protocol WeatherDelegate<NSObject>
-(void)getWeatherData:(YourNSObjectClass*)viewController getWeatherData:(NSMutableArray*)array;
#end
//NSObject.m file, in
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSString* cityName = [json objectForKey:#"name"];
int currentTempCelsius = (int)[[[json objectForKey:#"main"] objectForKey:#"temp"] intValue] - ZERO_CELSIUS_IN_KELVIN;
int maxTemp = (int)[[[json objectForKey:#"main"] objectForKey:#"temp_max"] intValue] - ZERO_CELSIUS_IN_KELVIN;
int minTemp = (int)[[[json objectForKey:#"main"] objectForKey:#"temp_min"] intValue] - ZERO_CELSIUS_IN_KELVIN;
NSString *weatherDescription = [[[json objectForKey:#"weather"] objectAtIndexBlush | :O ] objectForKey:#"description"];
weatherArray = [[NSMutableArray alloc] initWithObjects:cityName, weatherDescription,
[NSString stringWithFormat:#"%d", currentTempCelsius],
[NSString stringWithFormat:#"%d", maxTemp],
[NSString stringWithFormat:#"%d", minTemp],nil];
id<WeatherDelegate> strongDelegate = self.delegate;
if ([strongDelegate respondsToSelector:#selector(getWeatherData:getWeatherData:)])
{
[strongDelegate getWeatherData:self getWeatherData:weatherArray];
}
}
In yourViewController class,Add this WeatherData protocol and add the delegate function in .m file to fetch the data.
#interface yourViewControllerClass()<WeatherDelegate>
{
YourNSObjectClass *nsClass;
NSMutableArray *dataArray;
}
-(void)getWeatherData:(YourNSObjectClass*)viewController getWeatherData:(NSMutableArray*)array{
dataArray = [[NSMutableArray alloc]initWithArray:array];
}
-(void)searchButtonClicked:(UIButton*)sender
{
[self.view endEditing:YES];
WeatherData *weather = [[WeatherData alloc] init];
[weather getCurrentWeather:_textField.text];
self.weatherResultArray = dataArray;
//temperatureLabel.text = [NSString stringWithFormat:#"%d°",weather.currentTempCelsius];
}

nsmutable array not saving values

My array is not saving the values I put in it...
I am defining my nsmutablearray *arrayClientList in .h file
#interface StartupTableViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#property NSMutableArray *arrayClientList;
#property BOOL boolAddToClient;
//#property (strong, nonatomic) NSMutableArray *arrayAddClient;
#end
in .m file I am initializing like so
- (void)viewDidLoad {
[super viewDidLoad];
//initialize variables
self.arrayClientList = [[NSMutableArray alloc] init];
arraySelectedInformation = [[NSMutableArray alloc] init];
self.boolAddToClient = NO;
NSString *tstring = #"hello";
[self.arrayClientList addObject:tstring];
but then once I get to another method in this same class... the array is nil again. I must be doing something stupid for the array not to hold the values
-(void)viewDidAppear:(BOOL)animated{
//NSLog(#"appeared");
if (self.boolAddToClient) {
NSLog(#"add client to list");
self.boolAddToClient = NO;
[self.tableView reloadData];
}
else{
NSLog(#"startup");
}
}
I am trying to use it in another class
- (IBAction)buttonSubmit:(id)sender {
NSString *userDescription = [[NSString alloc] init];
NSString *userUsername = [[NSString alloc] init];
NSString *userPassword = [[NSString alloc] init];
userDescription = self.textfieldDescription.text;
userUsername = self.textfieldUserID.text;
userPassword = self.textfieldPW.text;
//check to make sure user filled out all fields
if (![userDescription isEqual:#""] && ![userUsername isEqual:#""] && ![userPassword isEqual: #""]){
NSLog(#"correct");
NSArray *arrayVC = self.navigationController.viewControllers;
StartupTableViewController *parentViewController = [arrayVC objectAtIndex:0];
parentViewController.boolAddToClient = YES;
NSMutableArray *arrayNewObjects = [[NSMutableArray alloc] initWithObjects:userDescription, userUsername, userPassword, nil];
NSMutableArray *tarray = parentViewController.arrayClientList;
[tarray addObject:arrayNewObjects];
[parentViewController.arrayClientList addObject:arrayNewObjects];
[self.navigationController popViewControllerAnimated:YES];
}
else{
NSLog(#"something missing");
}
}
Since I can't comment without rep, I must try with answer.
Try this:
In ViewDidLoad do alloc init with Strings you create in implementation and also change if block to this:
#implementation
{
NSString *userDescription;
NSString *userUsername;
NSString *userPassword;
}
-(void)viewDidLoad {
[super viewDidLoad];
NSString *userDescription = [[NSString alloc] init];
NSString *userUsername = [[NSString alloc] init];
NSString *userPassword = [[NSString alloc] init];
}
- (IBAction)buttonSubmit:(id)sender {
if (self.textfieldDescription.text.lenght != 0 && self.textfieldUserID.text.lenght != 0 && self.textfieldPW.text.lenght != 0) {
userDescription = self.textfieldDescription.text;
userUsername = self.textfieldUserID.text;
userPassword = self.textfieldPW.text;
....... and the rest
}
Please comment if it's not working, and I also think that you're not passing the informations right. Try searching an answer on how to pass arrays between TableViewControllers. Good Luck!

Use of undeclared identifier iOS When downloading Plist

This is driving me crazy, think it's a simple one but any help would be great, attempting to download a remote PLIST and use it to drive config inside the app....
Getting
IMProductsDataSource.m:57:11: error: expected identifier or '('
NSURL = *remoteURL = [NSURL URLWithString:#"IOS/"];
^
IMProductsDataSource.m:58:94: error: use of undeclared identifier 'remoteURL'
NSMutableDictionary *remoteDictionary = [NSMutableDictionary dictionaryWithContentsOfURL:remoteURL];
^
2 errors generated.
Any help would be amazing...
.H
#import <Foundation/Foundation.h>
#class ProductItem;
#interface IMProductsDataSource : NSObject
#property (nonatomic, strong) NSMutableArray* productsList;
#property (nonatomic, strong) ProductItem *selectedProduct;
+ (IMProductsDataSource *)sharedInstance;
#end
.M
#import "IMProductsDataSource.h"
#import "ProductItem.h"
#import "AppConstants.h"
#interface IMProductsDataSource ()
#property(nonatomic, assign) NSInteger currentRegion;
#end
#implementation IMProductsDataSource
+ (IMProductsDataSource *)sharedInstance
{
static IMProductsDataSource *instance = nil;
#synchronized(self) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[IMProductsDataSource alloc] init];
});
}
return instance;
}
-(id)init
{
if (self = [super init])
{
self.productsList = [[NSMutableArray alloc] init];
self.currentRegion = REGIONUK;
[self loadProducts];
}
return self;
}
-(void)loadProducts {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir = [path objectAtIndex:0];
NSString *filePath = [dir stringByAppendingPathComponent:#"Region1Products.plist"];
NSMutableDictionary *localDictionary;
NSURL = *remoteURL = [NSURL URLWithString:#"http://URL/IOS/"];
NSMutableDictionary *remoteDictionary = [NSMutableDictionary dictionaryWithContentsOfURL:remoteURL];
if(remoteDictionary != nil) {
[remoteDictionary writeToFile:filePath atomically:YES];
localDictionary = remoteDictionary;
}
else {
localDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
if(localDictionary == nil) localDictionary = [NSMutableDictionary dictionary];
}
// NSString *plistName = [NSString stringWithFormat:#"Region%dProducts", self.currentRegion];
// NSString *dataSourceFile = [[NSBundle mainBundle] pathForResource:
// ofType:#"plist"];
// NSArray* productsItems = [NSArray arrayWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
NSArray* productsItems = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
for (NSDictionary* productDictionary in productsItems) {
ProductItem* productItem = [[ProductItem alloc] init];
productItem.picturesCount = [productDictionary objectForKey:#"PicturesCount"];
productItem.maxPicturesCount = [productDictionary objectForKey:#"MaxPicturesCount"];
productItem.size = [productDictionary objectForKey:#"Size"];
productItem.previewImageName = [productDictionary objectForKey:#"ImageName"];
productItem.sequence = [productDictionary objectForKey:#"Sequence"];
productItem.productName = [productDictionary objectForKey:#"Name"];
productItem.type = [productDictionary objectForKey:#"ProductType"];
productItem.prices = [productDictionary objectForKey:#"Prices"];
productItem.shippingPrices = [productDictionary objectForKey:#"ShippingPrices"];
productItem.description = [productDictionary objectForKey:#"Description"];
productItem.popupMessage = [productDictionary objectForKey:#"PopupMessage"];
productItem.popupDetailMessage = [productDictionary objectForKey:#"PopupDetailMessage"];
productItem.incrementalPricing = [[productDictionary objectForKey:#"IncrementalPricing"] boolValue];
if (YES == productItem.incrementalPricing) {
productItem.incrementalPrices = [productDictionary objectForKey:#"IncrementalPrices"];
}
NSArray *previewItems = [productDictionary objectForKey:#"PreviewItems"];
for (NSDictionary* previewItem in previewItems) {
[productItem addProductPreviewItemFromDictionary:previewItem];
}
[self.productsList addObject:productItem];
}
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:#"sequence" ascending:YES];
self.productsList = [NSMutableArray arrayWithArray:[self.productsList sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]]];
}
#end
Your variable declaration isn't correct:
NSURL = *remoteURL = [NSURL URLWithString:#"http://URL/IOS/"];
Should be:
NSURL *remoteURL = [NSURL URLWithString:#"http://URL/IOS/"];
Note you have an extra equals sign after NSURL which is causing a syntax error.
That's a simple syntax error. This line from your code is not valid Objective-C:
NSURL = *remoteURL = [NSURL URLWithString:#"http://URL/IOS/"];
It looks like you just didn't want the first =.

CoreData, transient attribute and EXC_BAD_ACCESS.

I'm trying to build simple file browser and i'm stuck.
I defined classes, build window, add controllers, views.. Everything works but only ONE time.
Selecting again Folder in NSTableView or trying to get data from Folder.files causing silent EXC_BAD_ACCESS (code=13, address0x0) from main.
Info about files i keep outside of CoreData, in simple class, I don't want to save them:
#import <Foundation/Foundation.h>
#interface TPDrawersFileInfo : NSObject
#property (nonatomic, retain) NSString * filename;
#property (nonatomic, retain) NSString * extension;
#property (nonatomic, retain) NSDate * creation;
#property (nonatomic, retain) NSDate * modified;
#property (nonatomic, retain) NSNumber * isFile;
#property (nonatomic, retain) NSNumber * size;
#property (nonatomic, retain) NSNumber * label;
+(TPDrawersFileInfo *) initWithURL: (NSURL *) url;
#end
#implementation TPDrawersFileInfo
+(TPDrawersFileInfo *) initWithURL: (NSURL *) url {
TPDrawersFileInfo * new = [[TPDrawersFileInfo alloc] init];
if (new!=nil) {
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError * error;
NSDictionary * infoDict = [fileManager attributesOfItemAtPath: [url path] error:&error];
id labelValue = nil;
[url getResourceValue:&labelValue forKey:NSURLLabelNumberKey error:&error];
new.label = labelValue;
new.size = [infoDict objectForKey: #"NSFileSize"];
new.modified = [infoDict objectForKey: #"NSFileModificationDate"];
new.creation = [infoDict objectForKey: #"NSFileCreationDate"];
new.isFile = [NSNumber numberWithBool:[[infoDict objectForKey:#"NSFileType"] isEqualToString:#"NSFileTypeRegular"]];
new.extension = [url pathExtension];
new.filename = [[url lastPathComponent] stringByDeletingPathExtension];
}
return new;
}
Next I have class Folder, which is NSManagesObject subclass
// Managed Object class to keep info about folder content
#interface Folder : NSManagedObject {
NSArray * _files;
}
#property (nonatomic, retain) NSArray * files; // Array with TPDrawersFileInfo objects
#property (nonatomic, retain) NSString * url; // url of folder
-(void) reload; //if url changed, load file info again.
#end
#implementation Folder
#synthesize files = _files;
#dynamic url;
-(void)awakeFromInsert {
[self addObserver:self forKeyPath:#"url" options:NSKeyValueObservingOptionNew context:#"url"];
}
-(void)awakeFromFetch {
[self addObserver:self forKeyPath:#"url" options:NSKeyValueObservingOptionNew context:#"url"];
}
-(void)prepareForDeletion {
[self removeObserver:self forKeyPath:#"url"];
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == #"url") {
[self reload];
}
}
-(void) reload {
NSMutableArray * result = [NSMutableArray array];
NSError * error = nil;
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * percented = [self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray * listDir = [fileManager contentsOfDirectoryAtURL: [NSURL URLWithString: percented]
includingPropertiesForKeys: [NSArray arrayWithObject: NSURLCreationDateKey ]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:&error];
if (error!=nil) {NSLog(#"Error <%#> reading <%#> content", error, self.url);}
for (id fileURL in listDir) {
TPDrawersFileInfo * fi = [TPDrawersFileInfo initWithURL:fileURL];
[result addObject: fi];
}
_files = [NSArray arrayWithArray:result];
}
#end
In app delegate i defined
#interface TPAppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSArrayController * foldersController;
Folder * currentFolder;
}
- (IBAction)chooseDirectory:(id)sender; // choose folder
and
- (Folder * ) getFolderObjectForPath: path {
//gives Folder object if already exist or nil if not
.....
}
- (IBAction)chooseDirectory:(id)sender {
//Opens panel, asking for url
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:NO];
[panel setMessage:#"Choose folder to show:"];
NSURL * currentDirectory;
if ([panel runModal] == NSOKButton)
{
currentDirectory = [[panel URLs] objectAtIndex:0];
}
Folder * folderObject = [self getFolderObjectForPath:[currentDirectory path]];
if (folderObject) {
//if exist:
currentFolder = folderObject;
} else {
// create new one
Folder * newFolder = [NSEntityDescription
insertNewObjectForEntityForName:#"Folder"
inManagedObjectContext:self.managedObjectContext];
[newFolder setValue:[currentDirectory path] forKey:#"url"];
[foldersController addObject:newFolder];
currentFolder = newFolder;
}
[foldersController setSelectedObjects:[NSArray arrayWithObject:currentFolder]];
}
Please help ;)
Ha!
_files = [NSArray arrayWithArray:result];
Should be:
_files = [[NSArray arrayWithArray:result] retain];

AVAudioPlayer is leaking, where should i release it??

i m trying to play background.mp3 files as my game playback file ,and it works fine
but it leaking memory
#interface slots2ViewController : UIViewController <AVAudioPlayerDelegate>
{
AVAudioPlayer *PlayBack;
}
#property(nonatomic, retain) AVAudioPlayer *PlayBack ;
.m file
#synthesize PlayBack;
-(void)LoadnPlaySound
{
NSString *SubDir = [NSString stringWithFormat:#"AudioFiles/Theme%d",SlotId];
NSURL* file_url2 = nil;
file_url2 = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:#"background"ofType:#"mp3" inDirectory:SubDir ]];
AVAudioPlayer* TmpPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file_url2 error:nil];
self.PlayBack = TmpPlayer;
self.PlayBack.delegate = self;
[TmpPlayer release];
[self.PlayBack prepareToPlay];
[self.PlayBack play];
[release file_url2];
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)ThePlayer successfully:(BOOL)flag
{
[self.PlayBack play];
}
memory leak instrument says [self.PlayBack prepareToPlay] is the point of 100% leak
i m calling LoadnPlaySound whenever i m changing the theme.
also do i need to release self.PlayBack if yes then where