NSThread共享对象问题
我收到“EXC_BAD_ACCESS”,但无法弄清楚原因,任何帮助都会很大 - 提前谢谢您。
用户拍了一张照片,我想在另一个线程中对其进行一些处理...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
...
NSString *uid = [Asset stringWithUUID];
[_imageQueue setObject:img forKey:uid];
[NSThread detachNewThreadSelector:@selector(createAssetAsyncWithImage:)
toTarget:self
withObject:uid];
}
然后新线程
-(void) createAssetAsyncWithImage:(NSString *)uid {
NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];
if ([_imageQueue objectForKey:uid]) {
Asset *asset = [Asset createAssetWithImage:[_imageQueue objectForKey:uid]];
asset.uid = uid;
[self performSelectorOnMainThread:@selector(asyncAssetCreated:)
withObject:asset
waitUntilDone:YES];
}
[pool release];
}
调用
+(Asset *)createAssetWithImage:(UIImage *)img
{
....
UIImage *masterImg = [GraphicSupport createThumbnailFromImage:img pixels:img.size.height/2];
....
return newAsset;
}
然后这就是我不断收到 BAD_ACCESS 的地方
+(UIImage *)createThumbnailFromImage:(UIImage *)inImage pixels:(float)pixels{
CGSize size = image.size;
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = pixels / size.width;
}
else {
ratio = pixels / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rect.size);
//NSAssert(image,@"image NULL");
[image drawInRect:rect];
return UIGraphicsGetImageFromCurrentImageContext();
}
这是给我所有抱怨的图像...
我是什么我做错了??
预先非常感谢
I'm getting an “EXC_BAD_ACCESS” but just can't work out why, any help would be massive - thank you in advance.
The user takes a picture and I want to do some processing on it in another thread...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
...
NSString *uid = [Asset stringWithUUID];
[_imageQueue setObject:img forKey:uid];
[NSThread detachNewThreadSelector:@selector(createAssetAsyncWithImage:)
toTarget:self
withObject:uid];
}
Then the new thread
-(void) createAssetAsyncWithImage:(NSString *)uid {
NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];
if ([_imageQueue objectForKey:uid]) {
Asset *asset = [Asset createAssetWithImage:[_imageQueue objectForKey:uid]];
asset.uid = uid;
[self performSelectorOnMainThread:@selector(asyncAssetCreated:)
withObject:asset
waitUntilDone:YES];
}
[pool release];
}
Which calls
+(Asset *)createAssetWithImage:(UIImage *)img
{
....
UIImage *masterImg = [GraphicSupport createThumbnailFromImage:img pixels:img.size.height/2];
....
return newAsset;
}
And then this is where I keep getting the BAD_ACCESS
+(UIImage *)createThumbnailFromImage:(UIImage *)inImage pixels:(float)pixels{
CGSize size = image.size;
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = pixels / size.width;
}
else {
ratio = pixels / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rect.size);
//NSAssert(image,@"image NULL");
[image drawInRect:rect];
return UIGraphicsGetImageFromCurrentImageContext();
}
It's image that is giving me all the complaints...
What am I doing wrong??
Many thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现 UIGraphicsBeginImageContext 不是多线程的,不得不求助于 UIImage 的 Trevors fab 扩展。
http://vocaro.com /trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
I discovered that UIGraphicsBeginImageContext is not multi-threadable and had to resort to Trevors fab extensions to UIImage.
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/